阻止用户取消选择列表框中的项目?
我有一个列表框,里面有很多项目。用户可以单击某个项目来编辑其内容。如何防止用户取消选择所有项目?即,用户不应该无法选择任何内容。
I've got a ListBox with a bunch of items in it. The user can click an item to edit its contents. How do I prevent the user from deselecting all items? i.e., the user shouldn't be able to have nothing selected.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果您的
ListView
支持Multiple
选择,这对我有用。无论是
Single
还是Multiple
SelectionMode
,此解决方案都应该有效。如果您希望保留多个项目,请通过将对象添加回SelectedItems
属性来相应地调整代码。If your
ListView
supportsMultiple
selection, this worked for me.This solution should work regardless of
Single
orMultiple
SelectionMode
. If you have multiple items that you want them to be kept selected, adjust the code accordingly by adding your object back intoSelectedItems
property.这确实可以防止用户取消选择...将这 2 个事件添加到 checkListBox1 中,并在设计模式中将 属性 CheckOnClick 设置为“True”。 (MSVS2015)
This Works for Sure to Prevent User from Deselect... Add those 2 Events to your checkedListBox1 and set the Property CheckOnClick to "True" in Design Mode. (MSVS2015)
要禁用列表框/下拉列表中的一个或多个选项,您可以添加“已禁用”属性,如下所示。这会阻止用户选择此选项,并且它会显示灰色覆盖层。
To disable on or more options in your listbox/dropdown, you can add the "disabled" attribute as shown below. This prevent the user from selection this option, and it gets a gray overlay.
amccormack
建议的一种解决方案:One solution, as suggested by
amccormack
:我不确定是否有直接的方法来禁用取消选择项目,但对用户透明的一种方法是跟踪最后选择的项目,并且每当 SelectionChanged 事件引发并且所选索引为 -1,然后重新选择最后的值。
I'm not sure if there is a direct way to disable deselecting an Item, but one way which would be transparent to the user is to keep track of the last selected Item, and whenever the SelectionChanged event is raised and the selected index is -1, then reselect the last value.
您的情况缺少一种情况,即当列表被清除时,您将重新选择列表中不再存在的项目。我通过添加额外的检查来解决这个问题。
然后我将其放入行为中。
There is a case missing in your situation, which is when the list is cleared you will reselect an item there is no longer on the list. I solve this by adding an extra check.
I then put this inside a behaviour.