选择 WPF 列表视图项目时更改其背景

发布于 2024-12-05 01:45:31 字数 2696 浏览 0 评论 0原文

我有一个 WPF Listview,并且我已经重写了 ListView.ItemTemplate 以更改 ListViewItem.IsMouseOver 事件上的项目的背景颜色,如下所示:

<AlternationConverter x:Key="BackgroundConverter">
    <SolidColorBrush>White</SolidColorBrush>
    <SolidColorBrush>
        <SolidColorBrush.Color>
            <Color A="242" R="242" G="242" B="242" />
        </SolidColorBrush.Color>
    </SolidColorBrush>
</AlternationConverter>

    <Style x:Key="alternatingWithBinding" TargetType="{x:Type ListViewItem}">
        <Setter Property="Height" Value="31"/>
        <Setter Property="Background" 
          Value="{Binding RelativeSource={RelativeSource Self},
                 Path=(ItemsControl.AlternationIndex),
                 Converter={StaticResource BackgroundConverter}}"/>
        <Style.Triggers>
            <Trigger Property="ListViewItem.IsSelected" Value="True">
                <Setter Property="ListViewItem.Background" Value="Yellow" />
            </Trigger>
            <Trigger Property="ListBoxItem.IsMouseOver" Value="True">
                <Setter Property="ListBoxItem.Background" Value="Blue" />
            </Trigger>
        </Style.Triggers>
    </Style>

我想要实现的是将鼠标悬停在已选择的项目上时具有不同的颜色项目(黄色)。因此,在所有项目上,它将是蓝色悬停,而在选定的黄色项目上,它将是绿色。 我使用 MultiTrigger 尝试了以下尝试,但这并没有成功:

        <Style x:Key="alternatingWithBinding" TargetType="{x:Type ListViewItem}">
        <Setter Property="Height" Value="31"/>
        <Setter Property="Background" 
          Value="{Binding RelativeSource={RelativeSource Self},
                 Path=(ItemsControl.AlternationIndex),
                 Converter={StaticResource BackgroundConverter}}"/>
        <Style.Triggers>
            <Trigger Property="ListViewItem.IsSelected" Value="True">
                <Setter Property="ListViewItem.Background" Value="Yellow" />
            </Trigger>
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="ListBoxItem.IsMouseOver" Value="True"/>
                    <Condition Property="ListBoxItem.IsSelected" Value="True"/>
                </MultiTrigger.Conditions>
                <Setter Property="ListBoxItem.Background" Value="Green" />
            </MultiTrigger>
            <Trigger Property="ListBoxItem.IsMouseOver" Value="True">
                <Setter Property="ListBoxItem.Background" Value="Blue" />
            </Trigger>
        </Style.Triggers>
    </Style>

有什么想法吗? 谢谢。

I have a WPF Listview, and I have overridden the ListView.ItemTemplate to change the background color of the items on the ListViewItem.IsMouseOver event like so:

<AlternationConverter x:Key="BackgroundConverter">
    <SolidColorBrush>White</SolidColorBrush>
    <SolidColorBrush>
        <SolidColorBrush.Color>
            <Color A="242" R="242" G="242" B="242" />
        </SolidColorBrush.Color>
    </SolidColorBrush>
</AlternationConverter>

    <Style x:Key="alternatingWithBinding" TargetType="{x:Type ListViewItem}">
        <Setter Property="Height" Value="31"/>
        <Setter Property="Background" 
          Value="{Binding RelativeSource={RelativeSource Self},
                 Path=(ItemsControl.AlternationIndex),
                 Converter={StaticResource BackgroundConverter}}"/>
        <Style.Triggers>
            <Trigger Property="ListViewItem.IsSelected" Value="True">
                <Setter Property="ListViewItem.Background" Value="Yellow" />
            </Trigger>
            <Trigger Property="ListBoxItem.IsMouseOver" Value="True">
                <Setter Property="ListBoxItem.Background" Value="Blue" />
            </Trigger>
        </Style.Triggers>
    </Style>

What I'm trying to achieve is to have a different color when hovered over the already selected item (which is yellow). So on all the items it will be a Blue hover-over, and on the selected Yellow item, it'll be green.
I tried the below attempt, using MultiTrigger, but that didn't do the trick:

        <Style x:Key="alternatingWithBinding" TargetType="{x:Type ListViewItem}">
        <Setter Property="Height" Value="31"/>
        <Setter Property="Background" 
          Value="{Binding RelativeSource={RelativeSource Self},
                 Path=(ItemsControl.AlternationIndex),
                 Converter={StaticResource BackgroundConverter}}"/>
        <Style.Triggers>
            <Trigger Property="ListViewItem.IsSelected" Value="True">
                <Setter Property="ListViewItem.Background" Value="Yellow" />
            </Trigger>
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="ListBoxItem.IsMouseOver" Value="True"/>
                    <Condition Property="ListBoxItem.IsSelected" Value="True"/>
                </MultiTrigger.Conditions>
                <Setter Property="ListBoxItem.Background" Value="Green" />
            </MultiTrigger>
            <Trigger Property="ListBoxItem.IsMouseOver" Value="True">
                <Setter Property="ListBoxItem.Background" Value="Blue" />
            </Trigger>
        </Style.Triggers>
    </Style>

Any ideas?
Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苍白女子 2024-12-12 01:45:31

触发器是否按顺序应用?如果您将 MultiTrigger 移动到底部,它可能会起作用,然后它将在 IsMouseOver 触发器之后应用。

Are the triggers are applied in order? It might work if you move the MultiTrigger to the bottom, then it will apply after the IsMouseOver Trigger.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文