我有一个列表框,其中项目的背景颜色绑定到项的某些属性:饲养coustum风格时选择
<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>
这个工作,但是当我鼠标或选择项目背景变化(可能不会令人惊讶)默认的鼠标悬停/选择的颜色。我是新来的WPF,我不知道我正在做正确的这种事情,我想也许我需要使用ItemContainerStyleSelector,但我很困惑如何使用它,并且似乎很愚蠢的创建一个类只是为了这个小东西...
我还以为是创建一个IVALUEConverter从布尔到颜色,然后绑定,而不必使用DataTrigger作为一种不同的方法,那会更优雅吗?会有一些如何帮助我解决这个问题?
编辑:
这也将是很好,如果我可以选择的项目的背景颜色更改为基于AnotherPropertyOfFoo不同的颜色,如果不是过分的要求
编辑2 (延伸到@Sheridan回答评论):
这不起作用
<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>
嗨,感谢您的回复,但这似乎没有任何效果。 – pseudoDust
对不起,我不确定我在做什么错,但是这根本没有任何效果。我已经隔离了最简单的例子,我可以创建它,但它仍然不起作用,请参阅我使用的代码编辑我的文章,也许你可以找到我的错误 – pseudoDust