Search code examples
wpffocusvisualstyle

WPF - FocusVisualStyle where do i apply it?


I have a UserControl which basically wraps a ListBox like this -

        <ListBox x:Name="lb" ItemsSource="{Binding ElementName=UC,Path=Pages}"
             Background="{Binding ElementName=UC,Path=Background}"
             BorderBrush="Transparent"
             ScrollViewer.CanContentScroll="False" 
             ScrollViewer.HorizontalScrollBarVisibility="Hidden" 
             ScrollViewer.VerticalScrollBarVisibility="Disabled">

        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Horizontal" IsItemsHost="True"/>
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>

        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid Width="{Binding ElementName=UC,Path=ActualWidth}">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition MinWidth="20"/>
                        <ColumnDefinition/>
                        <ColumnDefinition MinWidth="20"/>
                    </Grid.ColumnDefinitions>
                    <ContentPresenter Grid.Column="1" Content="{Binding}"/>
                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

I need to set the FocusVisualStyle to {x:Null} to hide this functionality but no matter where i apply it, i still get the default blue selection color. I've tried setting it on the ListBox, StackPanel and the Grid but to no avail.

Any help would be great. thanks.


Solution

  • FocusVisualStyle applies the "marching ants" around the focused element, not the background color. To change the background color of selected ListBoxItems, do something like:

    <ListBox>
        <ListBox.Resources>
            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Value="Red"/>
            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Value="Black"/>
        </ListBox.Resources>    
    </ListBox>