8

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.

I've tried following steps:

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

Thanks in Advance! :)

akjoshi
  • 15,374
  • 13
  • 103
  • 121
Andy
  • 314
  • 3
  • 4
  • 11
  • 3
    Nice first question Andy, well constructed with precise examples of exactly what you were trying to highlight! +1 – Lloyd Powell Jun 22 '12 at 08:53
  • @Andy What is currentField1 in your converter? How are you getting this in Converter? Can you try binding to `currentField `(i.e. YourViewModelProperty) in the style invisible provided. – akjoshi Jun 22 '12 at 13:46
  • currentField is an object. Class name is Field and is has a Boolean Property named "Save". how can i bind to it in XAML? – Andy Jun 22 '12 at 14:05
  • Sounds like you need a multivalueconverter which checks IsSelected and your bool – patrick Mar 23 '17 at 17:52

2 Answers2

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> 
SwDevMan81
  • 48,814
  • 22
  • 151
  • 184
Niki Nikpour
  • 211
  • 2
  • 4
  • thanks for your answer, but this doesnt solve my problem. I know these lines, but I have an existing dependency between the color (your example: color="Red") and an Boolean value currentField.Save, an object in runtime. If it is false, then the color should be yellow and if it is true, the color should be green. – Andy Jun 26 '12 at 09:13
  • This no longer applies for Windows-8 which uses static colors in the ControlTemplate triggers. Consider https://stackoverflow.com/a/62714750/417939 instead. – YantingChen Feb 09 '21 at 03:31
0

If you wish to disable the highlighting when a listboxitem is selected or mouse over, you can use the below code.

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