【问题标题】:ListBox Selected Items Includes All ItemsListBox Selected Items 包括所有项目
【发布时间】:2020-11-13 20:37:23
【问题描述】:

在用户单击 CheckedListBox 中的任意数量的项目后,我想在窗口关闭时以编程方式删除对这些项目的检查。使用名为 lstChoices 的 CheckedListBox,我有:

For I As Integer = 0 To lstChoices.SelectedItems.Count - 1
    Dim lbi As Xceed.Wpf.Toolkit.Primitives.SelectorItem = CType(lstChoices.ItemContainerGenerator.ContainerFromIndex(I), Xceed.Wpf.Toolkit.Primitives.SelectorItem)
    lbi.IsSelected = False
Next

问题是 SelectedItems 属性不是选定的项目。奇怪的是,SelectedItems.Count 属性计算正确,但循环只会遍历第一个 ListBoxItems 直到 Count 是否被选中。

文档说 SelectedItems 属性“获取选中项的集合”。文档错误或该控件中存在错误。我怎样才能只得到选中的项目。

【问题讨论】:

    标签: wpf vb.net checkedlistbox xceed


    【解决方案1】:

    您当前正在迭代Items 集合,因为ContainerFromIndex 返回一个基于Items 属性而不是SelectedItems 属性的项目。

    您应该迭代 SelectedItems 并改用 lstChoices.ItemContainerGenerator.ContainerFromItem

    For index As Integer = 0 To lstChoices.SelectedItems - 1
      Dim selectedItem = lstChoices.SelectedItems(index)
      Dim selectedItemContainer = TryCast(lstChoices.ItemContainerGenerator.ContainerFromItem(selectedItem), Selector)
      selectedItemContainer.IsSelected = False
    Next
    

    【讨论】:

    • 标记为答案。更清楚地说(无论如何对我来说)是 For...Next 循环的索引不是在选定的项目上,而是在所有项目上。
    【解决方案2】:

    您必须小心区分 SelectedItems 和 Items 的元素。

    试试这个:

     For I As Integer = 0 To lstchoices.SelectedItems.Count - 1
        lstchoices.SetItemCheckState(lstchoices.Items.IndexOf(lstchoices.SelectedItems(I)), CheckState.Unchecked)
     Next
    

    【讨论】:

    • 那是一个表格 CheckedListBox。据我所知,wpf 没有等价物。另外,不处理 SelectedItems 不返回“选中项”的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-18
    • 1970-01-01
    • 2017-01-17
    • 1970-01-01
    • 2011-05-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多