阻止用户取消选择列表框中的项目?

发布于 2024-11-04 11:05:57 字数 71 浏览 0 评论 0原文

我有一个列表框,里面有很多项目。用户可以单击某个项目来编辑其内容。如何防止用户取消选择所有项目?即,用户不应该无法选择任何内容。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

乖乖兔^ω^ 2024-11-11 11:06:36

如果您的 ListView 支持Multiple 选择,这对我有用。

var found = e.RemovedItems.Cast<User>()
                          .Where(user => user.Id == Id);
if (found.Any())
{
    User user = found.First();
    user.Selected = true;
    (e.Source as ListView).SelectedItems.Add(user);
}

无论是 Single 还是 Multiple SelectionMode,此解决方案都应该有效。如果您希望保留多个项目,请通过将对象添加回 SelectedItems 属性来相应地调整代码。

If your ListView supports Multiple selection, this worked for me.

var found = e.RemovedItems.Cast<User>()
                          .Where(user => user.Id == Id);
if (found.Any())
{
    User user = found.First();
    user.Selected = true;
    (e.Source as ListView).SelectedItems.Add(user);
}

This solution should work regardless of Single or Multiple SelectionMode. If you have multiple items that you want them to be kept selected, adjust the code accordingly by adding your object back into SelectedItems property.

倾城花音 2024-11-11 11:06:35

这确实可以防止用户取消选择...将这 2 个事件添加到 checkListBox1 中,并在设计模式中将 属性 CheckOnClick 设置为“True”。 (MSVS2015)

        private void checkedListBox1_SelectedValueChanged(object sender, EventArgs e)
        {
            checkedListBox1.SetItemChecked(checkedListBox1.SelectedIndex, true);
        }

        private void checkedListBox1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            checkedListBox1.SetItemChecked(checkedListBox1.SelectedIndex, true);
        }

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)

        private void checkedListBox1_SelectedValueChanged(object sender, EventArgs e)
        {
            checkedListBox1.SetItemChecked(checkedListBox1.SelectedIndex, true);
        }

        private void checkedListBox1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            checkedListBox1.SetItemChecked(checkedListBox1.SelectedIndex, true);
        }
云朵有点甜 2024-11-11 11:06:35

要禁用列表框/下拉列表中的一个或多个选项,您可以添加“已禁用”属性,如下所示。这会阻止用户选择此选项,并且它会显示灰色覆盖层。

ListItem item = new ListItem(yourvalue, yourkey);
item.Attributes.Add("disabled","disabled");
lb1.Items.Add(item);

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.

ListItem item = new ListItem(yourvalue, yourkey);
item.Attributes.Add("disabled","disabled");
lb1.Items.Add(item);
彼岸花似海 2024-11-11 11:06:35

amccormack 建议的一种解决方案:

    private void hostsListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if(lstHosts.SelectedItem == null)
        {
            if(e.RemovedItems.Count > 0)
            {
                lstHosts.SelectedItem = e.RemovedItems[0];
            }

One solution, as suggested by amccormack:

    private void hostsListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if(lstHosts.SelectedItem == null)
        {
            if(e.RemovedItems.Count > 0)
            {
                lstHosts.SelectedItem = e.RemovedItems[0];
            }
淡笑忘祈一世凡恋 2024-11-11 11:06:33

我不确定是否有直接的方法来禁用取消选择项目,但对用户透明的一种方法是跟踪最后选择的项目,并且每当 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.

心在旅行 2024-11-11 11:06:32

您的情况缺少一种情况,即当列表被清除时,您将重新选择列表中不再存在的项目。我通过添加额外的检查来解决这个问题。

        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;
                }
            }
        }

然后我将其放入行为中

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;
                }
            }
        }

I then put this inside a behaviour.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文