WPF更改ListboxItem选中时突出显示颜色 [英] WPF Changing ListboxItem Highlight Color when Selected

查看:108
本文介绍了WPF更改ListboxItem选中时突出显示颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在WPF中设置 Listbox SelectedItem HighlightBrushKey 时遇到问题.我的意图是根据代码中给定的布尔值设置项目的颜色.

I've got a problem with setting the HighlightBrushKey of a SelectedItem of a Listbox in WPF. My intention was to set the color of an Item depending on a given Boolean value, lying in code.

我尝试了以下步骤:

  • 实现Converter,检查布尔值并返回正确的颜色.

  • Implementing a Converter, checking the boolean and returning the right color.

示例:

<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 ".

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".

转换器:

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 .这里的问题是我的物品变成白色并且看不到背景色

  • 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

    示例:

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

    这篇关于WPF更改ListboxItem选中时突出显示颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆