不允许取消选择/取消选择列表框不允许、列表

2023-09-03 01:38:50 作者:若只如初見°

有没有一种方法来配置WPF 列表框,使得它不可能取消选择/取消选择一个项目?所以总有一个项目选择?

Is there a way to configure the WPF ListBox such that it's not possible to deselect/unselect an item? So there is always an item selected?

我的列表框绑定到的ObservableCollection< MyClass的>

推荐答案

您可以处理的SelectionChanged 事件,并选择是否的SelectedItem 的计算结果为。要重新选择已为未选中,你可以跟踪最后选择的项目在私人领域的项目,应该始终在的SelectionChanged 事件进行更新。

You can handle the SelectionChanged event and set a selection if the SelectedItem evaluates to null. To reselect an item that has been unselected you can keep track of the last selected item in a private field, which should always be updated in the SelectionChanged event.