WPF列表框在鼠标悬停时选择项目 [英] WPF listbox select item on mouse over

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

问题描述

我正在尝试为列表框创建样式,以将选定的项目设置为当鼠标悬停在该项目上时的项目.

I'm trying to make a style for a listbox which will set the selected item to an item when the item has the mouse on it.

有任何提示吗?

推荐答案

您可以使用影响其所有项目的ListBox本身的样式来做到这一点:

You can do it using a style in the ListBox itself that affects all its items:

<ListBox.Resources>
    <Style TargetType="ListBoxItem" BasedOn="{StaticResource {x:Type ListBoxItem}}">
        <Style.Triggers>
            <DataTrigger Binding="{Binding IsMouseOver,RelativeSource={RelativeSource Self}}" 
                         Value="True">
                <Setter Property="IsSelected" Value="True" />
            </DataTrigger>
        </Style.Triggers>
    </Style>
</ListBox.Resources>

当IsMouseOver属性为true时,会将项目的IsSelected属性设置为true.如果您的ListBox不是多选的,它将按您期望的方式工作.

That'll set the IsSelected property on the item to true when the IsMouseOver property is true. Provided your ListBox isn't multi-select it works the way you'd expect.

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

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