鼠标悬停在 WPF 的列表框中选择项目 [英] Mouseover to select item in listbox in WPF

查看:19
本文介绍了鼠标悬停在 WPF 的列表框中选择项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 WPF 比较陌生,但我想知道如何启用列表框以基于鼠标悬停事件而不是按钮单击来选择项目.我希望在鼠标悬停在所选项目上时选择该项目,而不必先按单击.

I am relatively new to WPF, but I would like to know how can I enable a listbox to select an item based on a mouseover event instead of the button click. I would like the item to be selected when the mouse is over the selected item, without having to press click first.

谢谢

推荐答案

您可以编写一个简单的 ListBoxItem 样式,并在 IsMouseOver 属性上设置一个触发器来设置 IsSelected 属性:

You may write a simple ListBoxItem Style with a Trigger on the IsMouseOver property that sets the IsSelected property:

<ListBox>
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="IsSelected" Value="True"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>

这篇关于鼠标悬停在 WPF 的列表框中选择项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆