[英]Keeping coustum style of ListBoxItem when selected
I have a ListBox where the background color of the items are bound to some property of the entry: 我有一个ListBox,其中项目的背景色绑定到条目的某些属性:
<ListBox ItemsSource="{Binding ObservableCollectionOfFoos}" >
<ListBox.ItemContainerStyle >
<Style TargetType="ListBoxItem" >
<Setter Property="Content" Value="{Binding SomePropertyOfFoo}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding AnotherPropertyOfFoo}" Value="true">
<Setter Property="Background" Value="Green" />
</DataTrigger>
</Style.Triggers>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
This works, but when I mouse over or select an item the background changes (unsurprisingly perhaps) to the default mouse over / selected color. 这可行,但是当我将鼠标悬停或选择一个项目时,背景会更改(默认为鼠标悬停/选定的颜色)(这并不奇怪)。
I'm new to WPF and I'm not sure I'm going about doing this kind of thing correctly, I thought maybe I need to use ItemContainerStyleSelector, but I'm confused as to how to use it, and it seems silly to have to create a class just for this small thing... 我是WPF的新手,我不确定自己是否会正确地执行此类操作,我想也许我需要使用ItemContainerStyleSelector,但是我对如何使用它感到困惑,这似乎很愚蠢。必须为这个小东西创建一个类...
What I also thought was to create an IValueConverter from boolean to the color, and then bind though it without having to use DataTrigger as a different approach, woud that be more elegant? 我还想到的是创建一个从布尔到颜色的IValueConverter,然后通过绑定它而不必使用DataTrigger作为其他方法,这会更优雅吗? would that some how help me with this problem? 那会对我有什么帮助吗?
edit: 编辑:
It would also be nice if I could change the background color of the selected item to a different color based on AnotherPropertyOfFoo, if it's not too much to ask 如果我可以不问太多的话,如果我可以将所选项目的背景颜色更改为基于AnotherPropertyOfFoo的另一种颜色,那也很好。
edit 2 (extension to comment on @Sheridan answer): 编辑2(扩展名@Sheridan的评论):
this does not work 这不起作用
<ListBox>
<ListBox.Items>
<ListBoxItem>one</ListBoxItem>
<ListBoxItem>two</ListBoxItem>
<ListBoxItem>three</ListBoxItem>
<ListBoxItem>four</ListBoxItem>
</ListBox.Items>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="Background" Value="Green" />
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red" />
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Red" />
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black" />
<SolidColorBrush x:Key="{x:Static SystemColors.ControlTextBrushKey}" Color="Black" />
</Style.Resources>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
You can also override Template of ListBoxItem, extract default using blend and override or use some already mentioned here. 您还可以覆盖ListBoxItem的模板,使用blend提取默认值并覆盖或使用此处已经提到的一些方法 。
Edit 编辑
Actually it is not so hard to override Template :) and I guess it is most correct way to solve your problem. 实际上,重写Template :)并不难,我想这是解决您的问题的最正确方法。 Try this ItemContainer style. 试试这种ItemContainer样式。 It replaces default ListBoxItem style->Template. 它替换默认的ListBoxItem样式->模板。 To see how does it work - in triggers you can change any property of listbox item. 要查看其工作原理-在触发器中,您可以更改列表框项目的任何属性。
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Padding" Value="2,0,0,0"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Border Name="Border" Padding="2" SnapsToDevicePixels="true">
<ContentPresenter />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter TargetName="Border" Property="Background" Value="LightBlue"/>
</Trigger>
<Trigger Property="IsSelected" Value="true">
<Setter TargetName="Border" Property="Background" Value="Blue"/>
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="Gray"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.ItemContainerStyle>
The default style of ListBoxItem you can find here to make required modifications. 您可以在此处找到ListBoxItem的默认样式,以进行所需的修改。
Try using this: 尝试使用此:
<ListBox ItemsSource="{Binding ObservableCollectionOfFoos}" >
<ListBox.ItemContainerStyle >
<Style TargetType="ListBoxItem" >
<Setter Property="Content" Value="{Binding SomePropertyOfFoo}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding AnotherPropertyOfFoo}" Value="true">
<Setter Property="Background" Value="Green" />
</DataTrigger>
</Style.Triggers>
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" />
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black" />
<SolidColorBrush x:Key="{x:Static SystemColors.ControlTextBrushKey}" Color="Black" />
</Style.Resources>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
The SystemColors.HighlightBrushKey
represents the default background colour of the selected item in collection controls... here it is set to Transparent
, but you can set it to which ever colour you prefer. SystemColors.HighlightBrushKey
代表集合控件中所选项目的默认背景颜色...在这里,它设置为Transparent
,但是您可以将其设置为您喜欢的颜色。
UPDATE >>> 更新>>>
This code works just fine... if you change the first SolidColorBrush
in the Resources
section to Red
, then the selected item background colour will be Red
. 这段代码很好用...如果将“ Resources
部分中的第一个SolidColorBrush
更改为Red
,则所选项目的背景色将为Red
。 Your Binding
of AnotherPropertyOfFoo
will not affect the selected item as there is no relationship between the two. 您Binding
的AnotherPropertyOfFoo
不会影响所选的项目,因为两者之间没有任何关系。 To achieve that, you can try this instead: 为此,您可以尝试以下方法:
<ListBox ItemsSource="{Binding ObservableCollectionOfFoos}" >
<ListBox.ItemContainerStyle >
<Style TargetType="ListBoxItem" >
<Setter Property="Content" Value="{Binding SomePropertyOfFoo}"/>
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="{Binding AnotherPropertyOfFoo}" />
</DataTrigger>
</Style.Triggers>
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" />
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black" />
<SolidColorBrush x:Key="{x:Static SystemColors.ControlTextBrushKey}" Color="Black" />
</Style.Resources>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
Now the selected item will get the background colour from the AnotherPropertyOfFoo
property. 现在,所选项目将从AnotherPropertyOfFoo
属性获取背景颜色。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.