项目控件中的可变背景颜色
本文关键字:背景 颜色 控件 项目 | 更新日期: 2023-09-27 18:19:08
我想创建一个项目控件,该控件根据项目所属组的交替索引来替换项目的背景颜色。根据我下面列出的类别,我希望它的前三本书的背景是黑色的,然后当它遇到下一个出版商时,背景变回白色,依此类推,因为有许多独特的出版商。发布者的数量和名称是不确定的,仅在运行时计算。我已经尝试用xaml尽我所能做到这一点,但无论出于何种原因,似乎都不能为GroupItem访问alternationindex。如有任何帮助,不胜感激。
class Book
{
String Publisher {get; set;}
String Title {get; set;}
}
class ViewModel
{
var listBooks = new ObservableCollection<Book>();
listBooks.Add(new Book(){Publisher = "RandomHouse", Title = "Title1"});
listBooks.Add(new Book(){Publisher = "RandomHouse", Title = "Title2"});
listBooks.Add(new Book(){Publisher = "Penguin", Title = "Title5"});
ObservableCollection<Book> ListBookItems {get {return listBooks.Orderby(e => e.Publisher).ToList(); } }
}
<UserControl.Resources>
<Style TargetType="ItemsControl" x:Key="ListBookStyle">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<ScrollViewer CanContentScroll="True">
<ItemsPresenter/>
</ScrollViewer>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<VirtualizingStackPanel IsItemsHost="True"/>
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
<Setter Property="FontFamily">
<Setter.Value>Consolas</Setter.Value>
</Setter>
</Style>
<DataTemplate DataType="{x:Type models:Book}">
<Grid IsSharedSizeScope="True">
<Grid.ColumnDefinitions>
<ColumnDefinition SharedSizeGroup="Publisher" Width="100"/>
<ColumnDefinition SharedSizeGroup="Title" Width="100"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock
HorizontalAlignment="Left"
Text="{Binding Publisher}"
Grid.Column="0"
FontWeight="Bold"
Margin="5"/>
<TextBlock
HorizontalAlignment="Left"
Text="{Binding Title}"
Grid.Column="1"
FontWeight="Bold"
Margin="5"
/>
</Grid>
</DataTemplate>
<CollectionViewSource x:Key="ListBookItems" Source="{Binding ListBookItems}">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="Publisher"/>
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
</UserControl.Resources>
<DockPanel>
<ItemsControl
ItemsSource="{Binding Source={StaticResource ListBookItems}}"
Style="{StaticResource ListBookStyle}">
<ItemsControl.GroupStyle>
<GroupStyle AlternationCount="2">
<GroupStyle.ContainerStyle >
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Foreground" Value="#FF444444"/>
<Setter Property="Background" Value="#FF000000"/>
<!--<Style.Triggers>
<Trigger Property="AlternationIndex" Value="0">
<Setter Property="Foreground" Value="#FF444444"/>
<Setter Property="Background" Value="#FFD9D9D9"/>
</Trigger>
<Trigger Property="AlternationIndex" Value="1">
<Setter Property="Foreground" Value="#FF444444"/>
<Setter Property="Background" Value="#FFEFEFEF"/>
</Trigger>
</Style.Triggers>-->
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</ItemsControl.GroupStyle>
<ItemsControl.Template>
<ControlTemplate>
<ScrollViewer CanContentScroll="True">
<ItemsPresenter/>
</ScrollViewer>
</ControlTemplate>
</ItemsControl.Template>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel IsItemsHost="True"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</DockPanel>
你很接近了。您只需要在每个触发器中指定ItemsControl.AlternationIndex
作为属性名,而不仅仅是AlternationIndex
。
那就是说,坦率地说,我甚至重新考虑过提出这个答案,除了我实际上不能让它工作。也就是说,虽然我可以在调试器中看到它根据需要正确设置GroupItem.Background
属性值,但我在屏幕上看不到任何可见的效果。这就好像组的items-presenter忽略了GroupItem
的background属性。
我继续并将其作为答案的原因,即使它没有完全解决这个问题,也是因为在GroupItem
中获得GroupItem.Background
属性值受到实际演示者的尊重,这是一个完全不同的问题,而不是正确使用AlternationIndex
值。因此,虽然您可以使用这个答案来获得AlternationIndex
值的正确绑定,但您需要做更多的工作和/或发布另一个问题来深入研究为什么正确设置Background
属性值,这实际上并没有改变屏幕上显示的背景。
我希望我也能解释如何在GroupItem
中获得背景属性来影响屏幕上的显示。我希望如果你弄明白了,你至少会在这里发表评论,解释答案。