简体   繁体   English

在具有扩展选择模式的WPF列表框中取消选择

[英]Deselection on a WPF listbox with extended selection mode

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. 默认情况下,我似乎没有这种行为,我做了一个肮脏的hack,涉及到selectionchanged和mouseup来对其进行破解。 But there has to be a better way. 但是必须有更好的方法。 Any ideas? 有任何想法吗?

I've used myListBox. 我用过myListBox。 SelectedItems.Clear() . SelectedItems.Clear() Most selected items collections are read-only, but not the list boxes. 大多数选定的项目集合是只读的,但不是列表框。

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中的ListBoxItems会一直延伸到整个路径,这使得单击它非常困难。

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. 这是一个示例列表框,它修改了默认的ItemContainerStyle,以便项目仅占据列表的左侧,并且项目之间也有一定间隔。

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

To deselect the selected items we just need to set the SelectedItem to null in the EventHandler. 要取消选择所选项目,我们只需要在EventHandler中将SelectedItem设置为null。 When we click on a ListBoxItem, it will handle the MouseDown/Click etc to set the SelectedItem or modify the SelectedItems. 当我们单击ListBoxItem时,它将处理MouseDown / Click等以设置SelectedItem或修改SelectedItems。 Because of this, and the nature of the RoutedEvents we just handle the MouseDown in the ListBox exactly when we want. 因此,以及RoutedEvents的性质,我们仅在需要时才处理ListBox中的MouseDown。 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM