防止用户取消选择列表框中的项目? [英] Prevent user from deselecting an item in a ListBox?
本文介绍了防止用户取消选择列表框中的项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 ListBox,里面有一堆项目.用户可以单击一个项目来编辑其内容.如何防止用户取消选择所有项目?即,用户不应选择任何内容.
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.
推荐答案
您的情况缺少一个案例,即当列表被清除时,您将重新选择列表中不再存在的项目强>.我通过添加额外的检查来解决这个问题.
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.
var listbox = ((ListBox)sender);
if (listbox.SelectedItem == null)
{
if (e.RemovedItems.Count > 0)
{
object itemToReselect = e.RemovedItems[0];
if (listbox.Items.Contains(itemToReselect))
{
listbox.SelectedItem = itemToReselect;
}
}
}
然后我把这个放在一个行为中.
这篇关于防止用户取消选择列表框中的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文