具有扩展选择模式的 WPF 列表框上的取消选择 [英] Deselection on a WPF listbox with extended selection mode

查看:17
本文介绍了具有扩展选择模式的 WPF 列表框上的取消选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有扩展选择模式的简单列表框.选择工作几乎完美无缺,就像它在资源管理器中工作一样.但是取消选择并不是真的那么好.我想要的是,当我单击列表框中元素范围之外的内容时,我希望取消选择所有元素.默认情况下,我似乎并没有那样做,我做了一个肮脏的黑客,涉及 selectionchanged 和 mouseup 来破解它.但必须有更好的方法.有什么想法吗?

I have a simple listbox with extended selection mode. Selection works almost perfectly fine like it works in explorer. But deselection doesn't really work all that well. What I want is that when I click on something outside the range of elements in the listbox I want all elements to be deselected. I doesn't seem to behave that way by default and I did a dirty hack involving selectionchanged and mouseup to hack it up. But there has to be a better way. Any ideas?

推荐答案

添加取消选择功能并没有那么脏,而且您走在正确的轨道上.主要问题是,默认情况下,ListBox 内的 ListBoxItems 会一直延伸,使得点击一个非常困难.

It isn't that dirty to add in the deselection functionality, and you're on the right track. The main issue is that by default the ListBoxItems inside the ListBox will stretch all the way across, making it pretty tough to not click on one.

这是一个示例 ListBox,它修改了默认的 ItemContainerStyle,使项目仅占据列表的左侧,并且项目之间也有一些间距.

Here's an example ListBox that modifies the default ItemContainerStyle so that the items just take up the left side of the list and there is some spacing between the items as well.

<ListBox SelectionMode="Extended"
         Width="200" Mouse.MouseDown="ListBox_MouseDown">
    <ListBox.ItemContainerStyle>
        <Style TargetType="{x:Type ListBoxItem}">
            <Setter Property="Background"
                    Value="LightBlue" />
            <Setter Property="Margin"
                    Value="2" />
            <Setter Property="Padding"
                    Value="2" />
            <Setter Property="Width"
                    Value="100" />
            <Setter Property="HorizontalAlignment"
                    Value="Left" />
        </Style>
    </ListBox.ItemContainerStyle>
    <ListBoxItem >Item 1</ListBoxItem>
    <ListBoxItem >Item 2</ListBoxItem>
    <ListBoxItem >Item 3</ListBoxItem>
    <ListBoxItem >Item 4</ListBoxItem>
</ListBox>

要取消选择选定的项目,我们只需在 EventHandler 中将 SelectedItem 设置为 null.当我们点击一​​个 ListBoxItem 时,它会处理 MouseDown/Click 等来设置 SelectedItem 或修改 SelectedItems.正因为如此,以及 RoutedEvents 的性质,我们只是在需要的时候准确地处理 ListBox 中的 MouseDown.当单击 ListBox 内不属于某个项目的某个位置时.

To deselect the selected items we just need to set the SelectedItem to null in the EventHandler. When we click on a ListBoxItem, it will handle the MouseDown/Click etc to set the SelectedItem or modify the SelectedItems. Because of this, and the nature of the RoutedEvents we just handle the MouseDown in the ListBox exactly when we want. When somewhere inside the ListBox is clicked that isn't part of an item.

private void ListBox_MouseDown(object sender, MouseButtonEventArgs e)
{
    (sender as ListBox).SelectedItem = null;
}

这篇关于具有扩展选择模式的 WPF 列表框上的取消选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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