我在 WPF 中设置 Listbox
的 SelectedItem
的 HighlightBrushKey
遇到了问题。我的意图是根据代码中给定的布尔值来设置项的颜色。
我尝试了以下步骤:
Implementing a Converter, checking the boolean and returning the right color.
Example:
<ribbon:RibbonWindow.Resources> <l:WindowControl x:Key="ListBoxItemBackgroundConverter" /> <Style x:Key="listBoxStyle" TargetType="{x:Type ListBoxItem}"> <Style.Resources> <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="{Binding Source={x:Static SystemColors.HighlightBrushKey}, Converter={StaticResource ListBoxItemBackgroundConverter}}"/> <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="{Binding Source={x:Static SystemColors.ControlBrushKey}, Converter={StaticResource ListBoxItemBackgroundConverter}}"/> </Style.Resources> </Style> </ribbon:RibbonWindow.Resources>
The problem here was that the Convert method was called only once, but I need the Converter to be called every time I select an item and checking the Boolean. Like a Trigger, but with the "
HighlightBrushKey
".Converter:
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if(currentField == null) return Brushes.Yellow; if (currentField.Save) return Brushes.LightGreen; else return Brushes.Yellow; }
My next idea was setting "
HighlightBrushKey
" to "Transparent
" and changing theitem.Background
manually in code. The Problem here was that my items became white and the Background Color could not be seenExample:
<ListBox.Resources> <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" /> <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black" /> <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" /> </ListBox.Resources>
提前感谢!:)
currentField
(即YourViewModelProperty)吗? - akjoshi