ibrahimkhan
ibrahimkhan

Reputation:

listbox selected item should highlight with some color

I have a ListBox with some images. I want to hightlight the selected item with some color. I am using WwrapPanel to display images horizontally with a ScrollViewer. Is there any way to solve my problem?

Upvotes: 0

Views: 1083

Answers (2)

Carlo
Carlo

Reputation: 25969

This should do the trick:

<Window x:Class="StackOverflowTests.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" x:Name="window1" Height="300" Width="300">
    <Window.Resources>
        <!-- Specifies the Selection style of ListBoxItems. This changes the forced underlay colors from gray to transparent. -->
        <Style TargetType="ListBoxItem">
            <Style.Resources>
                <!-- This is the color used if the item is selected and the listbox has focus -->
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red"/>
            </Style.Resources>
        </Style>
    </Window.Resources>
    <Grid>
        <StackPanel Orientation="Vertical">
            <ListBox>
                <ListBoxItem Content="Item 1" />
                <ListBoxItem Content="Item 2" />
                <ListBoxItem Content="Item 3" />
                <ListBoxItem Content="Item 4" />
                <ListBoxItem Content="Item 5" />
                <ListBoxItem Content="Item 6" />
                <ListBoxItem Content="Item 7" />
                <ListBoxItem Content="Item 8" />
                <ListBoxItem Content="Item 9" />
                <ListBoxItem Content="Item 10" />
            </ListBox>
        </StackPanel>       
    </Grid>
</Window>

Upvotes: 1

Thomas Levesque
Thomas Levesque

Reputation: 292705

You should use a ItemContainerStyle with a trigger on the IsSelected property, and in the Trigger you put a setter on the Background property

Upvotes: 1

Related Questions