Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
hi everyone,
i searched for changing ListBoxItem back color and persisting this selection color even if the control lost focus
i found the solution like this :

XML
<ListBox Name="lst" >
           <ListBox.Resources>
               <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
                            Color="Black"/>
               <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}"
                            Color="Black"/>
           </ListBox.Resources>
           <ListBoxItem>battekha</ListBoxItem>
           <ListBoxItem>mooza</ListBoxItem>
           <ListBoxItem>manga</ListBoxItem>
       </ListBox>


but it still doesn't work for me
the selection while active or inactive remains the same as default

any idea why ?
Posted
Updated 5-Jul-19 1:03am
v2

C#
MyListBox.Resources[SystemColors.InactiveSelectionHighlightBrushKey] = SystemColors.HighlightBrush;
MyListBox.Resources[SystemColors.InactiveSelectionHighlightTextBrushKey] = SystemColors.HighlightTextBrush;

It works for me!
 
Share this answer
 
Comments
OriginalGriff 5-Jul-19 10:08am    
This isn't spam, and should not be reported as such.
Kats2512 5-Jul-19 10:10am    
Same answer posted to another question below:

https://www.codeproject.com/Questions/89340/Keep-ListViewItem-highlighted-after-loosing-focus
OriginalGriff 5-Jul-19 10:14am    
I know, and pretty much at the same time.
Please see my message in S&B.
OriginalGriff 5-Jul-19 10:07am    
While I applaud your urge to help people, it's a good idea to stick to new questions, rather than 5 year old ones. After that amount of time, it's unlikely that the original poster is at all interested in the problem any more!
Answering old questions can be seen as rep-point hunting, which is a form of site abuse. The more trigger happy amongst us will start the process of banning you from the site if you aren't careful. Stick to new questions and you'll be fine.
Hi Ahmad,

Please try the following code:

XML
<Grid>
    <ListBox Name="lst" >
        <ListBox.Resources>
            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
                         Color="Black"/>
            <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}"
                         Color="Black"/>
            <Style TargetType="{x:Type ListBoxItem}">
                    <Style.Triggers>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter Property="Foreground" Value="White"/> 
                    </Trigger>
                    </Style.Triggers>
                </Style>
        </ListBox.Resources>
        <ListBoxItem>battekha</ListBoxItem>
        <ListBoxItem>mooza</ListBoxItem>
        <ListBoxItem>manga</ListBoxItem>
    </ListBox>
    <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="107,157,0,0" Name="button1" VerticalAlignment="Top" Width="75" />
</Grid>
 
Share this answer
 
v2
Comments
Ahmad_kelany 12-May-14 9:43am    
Thanks for reply , Shai ,
But still , this code isn't working with me !!
I use visual studio 2013 , does this have something to do with anything ?
Shai Vashdi 12-May-14 9:53am    
Try on VS2010, if it's not working there maybe it's a different problem.
Shai Vashdi 12-May-14 10:40am    
P.S. It's more likely to assume that it's not working because of different Windows version/definitions, after all... SystemColors are the Windows defined colors.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900