WPF:取消选中列表框 [英] WPF: ListBox unselecting

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

问题描述

  1. 当用户单击我的单选ListBox中的某个项目时,该项目即被选中.
  2. 当用户第二次单击某个项目时,除非他们按住控制键,否则不会取消选择该项目.
  1. When the user clicks on a item in my single-select ListBox, the item is selected.
  2. When the user clicks on a item the second time, the item is not unselected unless they are holding the control key.

将#2更改为不需要控制键的推荐方法是什么?

What is the recommended way to change #2 to not require the control key?

推荐答案

确保选择模式为Multiple.通过选择多个:

Make sure the selection mode is Multiple. By selecting multiple:

您可以使用鼠标单击鼠标来选择和取消选择所需的任何项目.但是,如果一次只希望选择一个项目,则必须在SelectionChanged事件中取消选择代码中的其他项目.

you can use the mouse to select and deselect any item(s) you want with just a mouseclick. But if you want only 1 selected item at a time, you'll have to deselect the other items in code in the SelectionChanged event.

Private Sub MainList_SelectionChanged(ByVal sender As System.Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs)
    If e.AddedItems.Count > 0 Then
        Dim valid = e.AddedItems(0)
        For Each item In New ArrayList(MainList.SelectedItems)
            If item IsNot valid Then MainList.SelectedItems.Remove(item)
        Next
    End If
End Sub

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

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