Page 1 of 1

Setting a ListBox ItemContainerStyles's FocusVisualStyle to Null turns the items purple

Posted: 05 Feb 2021, 12:29
by Rocko Bonaparte
This isn't the end of the world (anymore) but I was wondering how defining a style for a ListBoxItem where I turn the FocusVisualStyle Null will turn the whole mess purple. This is despite the items being buttons with a style defined:
<ListBox Name="ChoicesList"  Background="Transparent" Foreground="White" 
            ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Disabled"
            SelectedItem="{Binding SelectedItem}" ItemsSource="{Binding Choices}"
            PreviewKeyDown="ChoicesList_PreviewKeyDown">
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
            </StackPanel>
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Button Content="{Binding}" Click="Button_Click" Style="{StaticResource ResourceKey=SaveButtonStyle}" Background="Transparent" Foreground="White" MouseEnter="Button_MouseEnter"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
    <!--
        AND HE SHALL DON THE PURPLE! This doesn't seem to be sufficient to define the container style for some reason. It turns the controls purple.
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="FocusVisualStyle" Value="{x:Null}" />
        </Style>
    </ListBox.ItemContainerStyle>
    -->
</ListBox>
So I know I need to define control templates for my controls, but I was kind of surprised how it went down here. Is there a default container style that I didn't sufficiently override when I only set the FocusVisualStyle?

It came up because I've been interacting with some folks that like to get rid of the "marching ants" and I think I agree for what I'm trying to do.

Re: Setting a ListBox ItemContainerStyles's FocusVisualStyle to Null turns the items purple

Posted: 05 Feb 2021, 17:06
by sfernandez
You are missing the BasedOn to extend the application style (that should contain setters for many properties including the Template) with your local style:
<ListBox.ItemContainerStyle>
    <Style TargetType="ListBoxItem" BasedOn="{StaticResoruce {x:Type ListBoxItem}}">
        <Setter Property="FocusVisualStyle" Value="{x:Null}" />
    </Style>
</ListBox.ItemContainerStyle>