列表框所选项目应突出显示某种颜色

时间:2009-05-15 11:53:11

标签: c# wpf xaml listbox selecteditem

我有ListBox一些图像。我想用一些颜色突出显示所选项目。我正在使用WwrapPanel使用ScrollViewer水平显示图像。有什么方法可以解决我的问题吗?

2 个答案:

答案 0 :(得分:1)

你应该在IsSelected属性上使用带有触发器的ItemContainerStyle,并在触发器中将setter放在Background属性上

答案 1 :(得分:1)

这应该可以解决问题:

<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>