EightyOne Unite
EightyOne Unite

Reputation: 11805

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.

Upvotes: 3

Views: 8790

Answers (2)

Kent Boogaart
Kent Boogaart

Reputation: 178770

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>

Upvotes: 10

bendewey
bendewey

Reputation: 40245

Kent is correct the FocusVisualStyle is only related to the Keyboard focus, when selected controls with the Tab Key.

If you are just trying to display a list without any Selection capabilities you may just be able to downgrade your ListBox to an ItemsControl

<ItemsControl 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">
  <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Horizontal" IsItemsHost="True"/>
        </ItemsPanelTemplate>
  </ItemsControl.ItemsPanel>
  <!-- others -->
</ItemsControl>

Upvotes: 2

Related Questions