【问题标题】:WPF Changing ListboxItem Highlight Color when Selected选中时 WPF 更改 ListboxItem 突出显示颜色
【发布时间】:2012-06-24 13:30:16
【问题描述】:

我在 WPF 中设置 SelectedItemHighlightBrushKeyListbox 时遇到问题。我的意图是根据给定的布尔值设置项目的颜色,位于代码中。

我尝试了以下步骤:

  • 实现一个转换器,检查布尔值并返回正确的颜色。

    例子:

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

提前致谢! :)

【问题讨论】:

  • 第一个问题很好,安迪,用精确的例子很好地构建了你想要强调的内容! +1
  • @Andy 您的转换器中的 currentField1 是什么?你是如何在转换器中得到这个的?您可以尝试以提供的不可见样式绑定到currentField (即YourViewModelProperty)吗?
  • currentField 是一个对象。类名是 Field 并且有一个名为“Save”的布尔属性。如何在 XAML 中绑定它?
  • 听起来你需要一个多值转换器来检查 IsSelected 和你的布尔值

标签: wpf background-color listboxitem


【解决方案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="Red")和布尔值 currentField.Save(运行时对象)之间存在依赖关系。如果为假,则颜色应为黄色,如果为真,则颜色应为绿色。
  • 这不再适用于在 ControlTemplate 触发器中使用静态颜色的 Windows-8。请考虑使用stackoverflow.com/a/62714750/417939
【解决方案2】:

如果您希望在选择列表框项或鼠标悬停时禁用突出显示,您可以使用以下代码。

<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}"/>

【讨论】:

    猜你喜欢
    • 2019-12-24
    • 1970-01-01
    • 2019-03-05
    • 2014-05-30
    • 2010-12-12
    • 2017-03-24
    • 2019-07-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多