views:

1555

answers:

2

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.

+4  A: 

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>

HTH, Kent

Kent Boogaart
+1  A: 

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>
bendewey
Ja, had a go with that but I'm rendering UIElements in the ListBox and they don't obey the ItemTemplate in an ItemsControl oddly. Thanks for the help though.
Stimul8d