【问题标题】:listbox selected item should highlight with some color列表框所选项目应以某种颜色突出显示
【发布时间】:2010-10-26 11:27:12
【问题描述】:

我有一个ListBox,上面有一些图片。我想用某种颜色突出显示所选项目。我正在使用WwrapPanelScrollViewer 水平显示图像。有什么办法可以解决我的问题吗?

【问题讨论】:

  • 也许想检查这个问题的标题的拼写和语法

标签: c# wpf xaml listbox selecteditem


【解决方案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>

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 感谢您的回复。在您的帮助下,我得到了解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-29
    相关资源
    最近更新 更多