鼠标悬停在 WPF 的列表框中选择项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15511752/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 08:11:02  来源:igfitidea点击:

Mouseover to select item in listbox in WPF

wpflistboxmouseover

提问by user2096837

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.

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

Thank you

谢谢

回答by Clemens

You may write a simple ListBoxItem Style with a Trigger on the IsMouseOverproperty that sets the IsSelectedproperty:

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

<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>