WPF - FocusVisualStyle, where do I use it?

I have UserControlone that basically wraps ListBoxas follows:

        <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 FocusVisualStyleto {x:Null}to hide this functionality, but no matter where I use it, I still get the default highlight blue. I tried to install it in ListBox, StackPanel and Grid, but to no avail.

Any help would be great. thanks.

+3
source share
2 answers

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

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

Kent , FocusVisualStyle , .

- , ListBox 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>
+2

Source: https://habr.com/ru/post/1705190/


All Articles