none
WPF: how to make the selection item highlight color stay in the listBox even the listBox control loses the fucus?

    Question

  • In the listbox, when mouse over a selected row, it will display the selected row as the systemselection color (for example, Blue)
    However, when listbox lose the focus, the selected row become light gray. light gray color is not that obviously for distinguish selected one.
    Is there a way we can change the color setting (for example, to be Red), so even though the listbox loses the focus, the selected row still show the Red.
    thx!
    Wednesday, July 22, 2009 9:31 PM

Answers

  • Just add the following lines inside your <ListBox></ListBox> tag, and you'll have the selected item in red, even when ListBox loses focus:

    <ListBox.Resources>
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red"/>
        <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Red"/>
    </ListBox.Resources>

     

     


    • Edited by Datafeed Thursday, July 23, 2009 6:09 AM
    • Proposed as answer by Datafeed Thursday, July 23, 2009 6:11 AM
    • Marked as answer by Jim Zhou - MSFT Tuesday, July 28, 2009 11:52 AM
    Thursday, July 23, 2009 6:02 AM

All replies


  • Oscar Avarez Guerras - Arquitecto Software en I3B (I+D+I) Blog:http://geeks.ms/blogs/oalvarez Por favor marca como respuesta si te ha ayudado esta respuesta
    Wednesday, July 22, 2009 9:42 PM
  • Just add the following lines inside your <ListBox></ListBox> tag, and you'll have the selected item in red, even when ListBox loses focus:

    <ListBox.Resources>
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red"/>
        <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Red"/>
    </ListBox.Resources>

     

     


    • Edited by Datafeed Thursday, July 23, 2009 6:09 AM
    • Proposed as answer by Datafeed Thursday, July 23, 2009 6:11 AM
    • Marked as answer by Jim Zhou - MSFT Tuesday, July 28, 2009 11:52 AM
    Thursday, July 23, 2009 6:02 AM
  • Hi,

    this works good, but there is  another problem:

    If you want list box with behavior mentioned above, but each ListBoxItem has context menu, as you can see inexample below, the context menu is also red. It can be avoided by defining own style for ContextMenu, but in my app it must be taken from system theme. How can I avoid this?

    <Window x:Class="ListBox_styling.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525">

        <Window.Resources>
            <ContextMenu x:Key="contextMenu">
                <MenuItem>
                    <MenuItem.Header>
                        MenuItem 1
                    </MenuItem.Header>
                </MenuItem>
                <MenuItem>
                    <MenuItem.Header>
                        MenuItem 2
                    </MenuItem.Header>
                </MenuItem>
                <MenuItem>
                    <MenuItem.Header>
                        MenuItem 3
                    </MenuItem.Header>
                </MenuItem>
            </ContextMenu>
        </Window.Resources>
        
        <Grid>
            <ListBox>
                <ListBox.Resources>
                    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red"/>
                    <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Red"/>
                </ListBox.Resources>
                <ListBoxItem ContextMenu="{StaticResource contextMenu}">
                    ListBoxItem 1
                </ListBoxItem>
                <ListBoxItem ContextMenu="{StaticResource contextMenu}">
                    ListBoxItem 2
                </ListBoxItem>
            </ListBox>
        </Grid>
    </Window>

    Thursday, August 15, 2013 1:16 PM