WPF更改ListboxItem选中时的高亮颜色

8

我在 WPF 中设置 ListboxSelectedItemHighlightBrushKey 遇到了问题。我的意图是根据代码中给定的布尔值来设置项的颜色。

我尝试了以下步骤:

  • 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 the item.Background manually in code. The Problem here was that my items became white and the Background Color could not be seen

    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>
    

提前感谢!:)


3
安迪,你的第一个问题非常好,构建得很好,并且用精确的例子准确地突出了你想要强调的内容!+1 - Lloyd Powell
@Andy,你的转换器中的currentField1是什么?你是如何在转换器中获取它的?你可以尝试在提供的invisible样式中绑定到currentField(即YourViewModelProperty)吗? - akjoshi
currentField是一个对象。 类名为Field,它有一个名为“Save”的布尔属性。 我该如何在XAML中绑定它? - Andy
听起来你需要一个多值转换器,它可以检查IsSelected和你的bool值。 - patrick
2个回答

1
<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> 

谢谢您的回答,但这并没有解决我的问题。我知道这些代码行,但是在运行时我有一个颜色(color)和布尔值(currentField.Save)之间的依赖关系。如果它为false,则颜色应为黄色;如果它为true,则颜色应为绿色。 - Andy
这不再适用于使用静态颜色在ControlTemplate触发器中的Windows-8。请考虑使用https://dev59.com/_XI95IYBdhLWcg3w1BdN#62714750。 - YantingChen

0
如果您希望在选择列表框项或鼠标悬停时禁用高亮显示,可以使用以下代码。
<Style TargetType="ListBoxItem" x:Key="ListBoxItemStyle">
    <Setter Property="IsSelected" Value="{Binding Content.IsSelected, Mode=TwoWay, RelativeSource={RelativeSource Self}}"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ListBoxItem">
                <ContentPresenter/>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<ListBox ItemContainerStyle="{StaticResource ListBoxItemStyle}"/>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接