我在 WPF 中设置HighlightBrushKey
a SelectedItem
of a时遇到问题。Listbox
我的意图是根据给定的布尔值设置项目的颜色,位于代码中。
我尝试了以下步骤:
实现一个转换器,检查布尔值并返回正确的颜色。
例子:
<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>
这里的问题是 Convert 方法只被调用了一次,但是我每次选择一个项目并检查布尔值时都需要调用 Converter。类似于触发器,但带有“
HighlightBrushKey
”。转换器:
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; }
我的下一个想法是将“
HighlightBrushKey
”设置为“ ”并在代码中手动Transparent
更改。item.Background
这里的问题是我的物品变成了白色并且看不到背景颜色例子:
<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>
提前致谢!:)