我在设置时遇到问题HighlightBrushKey
of a SelectedItem
of a Listbox
在 WPF 中。我的目的是根据代码中给定的布尔值设置项目的颜色。
我尝试了以下步骤:
-
实现转换器,检查布尔值并返回正确的颜色。
例子:
<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
" to "Transparent
”并改变item.Background
手动在代码中。这里的问题是我的项目变成白色并且无法看到背景颜色
Example:
<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>
提前致谢! :)
<Style x:Key="listBoxStyle" TargetType="{x:Type ListBox}">
<Style.Resources>
<!-- Background of selected item when focussed -->
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red" />
<!-- Background of selected item when not focussed -->
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Green" />
</Style.Resources>
</Style>
<ListBox Style="{StaticResource listBoxStyle}">
</ListBox>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)