【问题标题】:How to highlight row for IsSelected如何突出显示 IsSelected 的行
【发布时间】:2012-12-31 18:45:58
【问题描述】:

当我从ListView 中选择时,我想突出显示该行,但我无法让它工作。谁能看看我所拥有的并告诉我我做错了什么?另一个问题是如何在我的 ViewModel 中拥有一个属性并根据 bool 值设置Background 颜色,如何实现?

<ListView.ItemContainerStyle>
    <Style TargetType="{x:Type ListViewItem}">
        <Style.Triggers>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="Background" Value="Green" />
            </Trigger>

            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Background" Value="Yellow" />
            </Trigger>
        </Style.Triggers>
    </Style>
</ListView.ItemContainerStyle>

【问题讨论】:

    标签: wpf xaml listview background itemcontainerstyle


    【解决方案1】:

    这里的问题是ListView 的项目模板会自动添加画笔类型SystemColors.HighlightBrushKey 的“选定突出显示”-“真正的解决方案”是覆盖项目模板定义,但一种方法是你可以得到什么你追求的是这样的:

    <ListView.ItemContainerStyle>
        <Style TargetType="{x:Type ListViewItem}">
            <Style.Resources>
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Green"/>
            </Style.Resources>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background" Value="Yellow" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </ListView.ItemContainerStyle>
    

    【讨论】:

      【解决方案2】:

      您可以使用名为IValueConverter 的接口将 Bool 值绑定到颜色画笔。

      这是一个相关的帖子:What is best practise for IValueConverter?

      【讨论】:

        【解决方案3】:
                    <ListView  Name="listBox1" ItemsSource="{Binding Path=SimpleList}"
                              HorizontalAlignment="Left" VerticalAlignment="Top" Background="Olive">
                        <ListView.ItemContainerStyle>
                            <Style TargetType="ListViewItem">
                                <EventSetter Event="MouseEnter" Handler="listBox1_ListBoxItem_MouseEnter"/>
                                <EventSetter Event="MouseLeave" Handler="listBox1_ListBoxItem_MouseLeave"/>
                                <Style.Resources>
                                    <!-- Background of selected item when focussed -->
                                    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
                                        Color="Green"/>
                                    <!-- Background of selected item when not focussed -->
                                    <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}"
                                        Color="Yellow" />
                                </Style.Resources>
                                <Style.Triggers>
                                    <Trigger Property="IsMouseOver" Value="True">
                                        <!--<Setter Property="FontWeight" Value="Bold" />-->
                                        <Setter Property="Background" Value="Orange" />
                                    </Trigger>
                                </Style.Triggers>
                            </Style>
                        </ListView.ItemContainerStyle>
                    </ListView>
        

        【讨论】:

        • 他为“IsMouseOver”设置的正常触发器应该可以正常工作 - 它是被 ItemTemplate 覆盖的选定样式。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-28
        • 1970-01-01
        相关资源
        最近更新 更多