sergun

Hello!

How can I specify the color of highlighted text in non-focused listbox

The color of text of highlighted alternative when listbox owns the focus is controlled by:

SystemColors.HighlightTextBrushKey

But when listbox loses focus the color is switched back to

SystemColors.ControlTextBrushKey value.

How can I keep it



Re: Windows Presentation Foundation (WPF) Color of highlighted text in non-focused listbox.

Benny Tops

Hi,

Adjust the ControlTemplate of the ListBoxItem to exactly the behavior you want.

Best regards,

Benny





Re: Windows Presentation Foundation (WPF) Color of highlighted text in non-focused listbox.

sergun

At the moment I try to control corresponding colors in such way:

<ListBox.Resources>

<SolidColorBrush x:Key="{xTongue Tiedtatic SystemColors.ControlBrushKey}" Color="Blue" />

<SolidColorBrush x:Key="{xTongue Tiedtatic SystemColors.HighlightBrushKey}" Color="Blue" />

<SolidColorBrush x:Key="{xTongue Tiedtatic SystemColors.HighlightTextBrushKey}" Color="White" />

</ListBox.Resources>

Should I really write ControlTemplate for my purpose





Re: Windows Presentation Foundation (WPF) Color of highlighted text in non-focused listbox.

Benny Tops

Hi,

No, in this case I wouldn't use a ControlTemplate either. For your purpose is what you are doing a very good choice. If you want to do this application wide (referring to your other post), just do this in the application's ResourceDictionary.

Best regards,

Benny





Re: Windows Presentation Foundation (WPF) Color of highlighted text in non-focused listbox.

sergun

The problem is that text has highlightedtextbrush color only when ListBox owns focus.

I need something like this, but it didn't work:

<ListBox.Resources>

<SolidColorBrush x:Key="{xTongue Tiedtatic SystemColors.ControlBrushKey}" Color="Blue" />

<SolidColorBrush x:Key="{xTongue Tiedtatic SystemColors.HighlightBrushKey}" Color="Blue" />

<SolidColorBrush x:Key="{xTongue Tiedtatic SystemColors.HighlightTextBrushKey}" Color="White" />

</ListBox.Resources>

<ListBox.Style>

<Style TargetType="{x:Type ListBox}">

<Style.Triggers>

<Trigger Property="IsFocused" Value="False">

<Setter Property="Foreground" Value="White"/>

</Trigger>

</Style.Triggers>

</Style>

</ListBox.Style>





Re: Windows Presentation Foundation (WPF) Color of highlighted text in non-focused listbox.

TorLanglo

Try using this instead:

Code Snippet

<ListBox.Resources>

<SolidColorBrush x:Key="{DynamicResource {xTongue Tiedtatic SystemColors.ControlBrushKey}}" Color="Blue" />

<SolidColorBrush x:Key="{DynamicResource {xTongue Tiedtatic SystemColors.HighlightBrushKey}}" Color="Blue" />

<SolidColorBrush x:Key="{DynamicResource {xTongue Tiedtatic SystemColors.HighlightTextBrushKey}}" Color="White" />

</ListBox.Resources>

Hope this helps,

Tor

--------

Tor Langlo
Koda Software
Web: http://www.kodasoftware.com
Blog: http://torlanglo.spaces.live.com





Re: Windows Presentation Foundation (WPF) Color of highlighted text in non-focused listbox.

sergun

It encourages error:

Error 3 A key for a dictionary cannot be of type 'System.Windows.DynamicResourceExtension'. Only String, TypeExtension, and StaticExtension are supported.





Re: Windows Presentation Foundation (WPF) Color of highlighted text in non-focused listbox.

TorLanglo

I'm sorry, but my code example was just plain wrong. Please Ignore!

Sergun, the example you posted above works when I test it with XamlPad. But, as you indicated in the original post, you can also use ControlTextBrushKey to control the foreground color when the ListBox doesn't have focus. Just add a

Code Snippet

<SolidColorBrush x:Key="{x:Static SystemColors.ControlTextBrushKey}" Color="White" />

to the other local resource, and you should be able to remove the trigger.

Hope this helps more,

Tor.





Re: Windows Presentation Foundation (WPF) Color of highlighted text in non-focused listbox.

sergun

Tor, thanks!

It now can be compiled successfully.

But now there is problem with the color of text of non-selected items

they are white as their background and not visible..

Probably ControlTextBrushKey should be even Blue and I should change it for selected item to White.

But don't know how Sad





Re: Windows Presentation Foundation (WPF) Color of highlighted text in non-focused listbox.

TorLanglo

Here's a larger ListBox test I've been working on tonight. This uses styles and triggers to control the foreground and background colors of the ListBoxItem elements. There are no templates involved. I've split the code into two posts (because of max. limits).

Tor.

Code Snippet

<StackPanel VerticalAlignment="Center" Background="Black">

<StackPanel.Resources>

<!-- Simulate the possibility of the application having a default ListBoxItem style. The

colors set here will be overridden, but the other setters will apply. -->

<Style TargetType="{x:Type ListBoxItem}">

<Setter Property="Foreground" Value="Orange" />

<Setter Property="Background" Value="Red" />

<Setter Property="Padding" Value="3" />

<Setter Property="Margin" Value="1" />

</Style>

</StackPanel.Resources>

<Button HorizontalAlignment="Center">Just something to focus on...</Button>

<!-- Let's say these are our requirements:

ListBox background: DarkGray

Focused Not Focused

************** ***************

Selected items: White on Blue Yellow on Magenta

Non-selected items: Blue on Silver Black on Yellow

-->

<ListBox Background="DarkGray" Margin="10" SelectedIndex="0" Padding="5"

HorizontalAlignment="Center" VerticalAlignment="Top">

<ListBox.Resources>

<!-- Workaround for setter that doesn't work below -->

<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Magenta" />

<Style TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource {x:Type ListBoxItem}}">

<!-- Defaults are set for non-selected item when listbox has focus -->

<Setter Property="Foreground" Value="Blue" />

<Setter Property="Background" Value="Silver" />

<Style.Triggers>

<!-- Triggered when item is both selected and has focus -->

<MultiTrigger>

<MultiTrigger.Conditions>

<Condition Property="IsSelected" Value="True"/>

<Condition Property="Selector.IsSelectionActive" Value="True"/>

</MultiTrigger.Conditions>

<Setter Property="Foreground" Value="White" />

<Setter Property="Background" Value="Blue" />

</MultiTrigger>





Re: Windows Presentation Foundation (WPF) Color of highlighted text in non-focused listbox.

TorLanglo

Code Snippet

<!-- Triggered when item is selected, but does not have focus -->

<MultiTrigger>

<MultiTrigger.Conditions>

<Condition Property="IsSelected" Value="True"/>

<Condition Property="Selector.IsSelectionActive" Value="False"/>

</MultiTrigger.Conditions>

<Setter Property="Foreground" Value="Yellow" />

<!-- This setter seems to be ignored!!!! -->

<Setter Property="Background" Value="Magenta" />

</MultiTrigger>

<!-- Triggered on non-selected items which do not have focus -->

<MultiTrigger>

<MultiTrigger.Conditions>

<Condition Property="IsSelected" Value="False"/>

<Condition Property="Selector.IsSelectionActive" Value="False"/>

</MultiTrigger.Conditions>

<Setter Property="Foreground" Value="Black"/>

<Setter Property="Background" Value="Yellow"/>

</MultiTrigger>

</Style.Triggers>

</Style>

</ListBox.Resources>

<ListBoxItem>Good golfing, Kristoffer</ListBoxItem>

<ListBoxItem>Hello world 2</ListBoxItem>

<ListBoxItem>Hello world 3</ListBoxItem>

<ListBoxItem>Hello world 4</ListBoxItem>

</ListBox>

</StackPanel>





Re: Windows Presentation Foundation (WPF) Color of highlighted text in non-focused listbox.

sergun

Thanks Tor!

You helped me a lot!!!

Sergey.