我有一個列表框,其中項目的背景顏色綁定到項的某些屬性:飼養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