灰显或阻止列表视图项目选择? [英] Greying out or preventing listview item selection?

查看:75
本文介绍了灰显或阻止列表视图项目选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。

我有一个ListView填充了几个项目(ListViewItem)项目。
我想用这些东西做两件事。

1)我想阻止某些项目被选中(基于我定义的一些标准)。





我的尝试:



listview1.item.selected = false

 listview1.SelectedItem.Selected = false 

解决方案

从我所看到的,你不能将列表视图项设置为不可选。



其他你可以做的事情;



a)为ItemSelectionChanged添加事件处理程序并在那里处理

b)使用复选框并为ItemCheck / ItemChecked事件添加事件处理程序



亲切的问候


这是一个非常简单的解决方案,使用ListBox控件的 SelectedIndexChanged 事件处理程序。



我正在检查以确保索引大于-1,因为这是一个无效索引(-1表示没有选择任何内容)。您可以根据需要对其进行修改,但因为它代表片段,因此任何单击我无法选择的列表框项目都将清除所有选定的项目。根据需要使用此事件处理程序。

提示:更改项目的背景颜色:p



 私人  Sub  ListBox1_SelectedIndexChanged(发件人作为 对象,e  As  EventArgs)句柄 ListBox1.SelectedIndexChanged 
If ListBox1.SelectedIndex> -1
ListBox1.Items(ListBox1.SelectedIndex)= 我无法选择 然后
ListBox1.ClearSelected()
结束 如果
结束 Sub


这是另一个使用checkbox和ItemCheck事件组合禁用listview项的示例。还有关于如何使项目变灰的说明。



c# - 如何在.net 3.5中禁用listview控件中的项目 - Stack Overflow [ ^ ]

Hi all

I have a ListView populated with several items (ListViewItem) items.
I want to do 2 things with these items.   

1) I want to prevent some items from being selected (based on some critera that I define). 



What I have tried:

listview1.item.selected=false

listview1.SelectedItem.Selected=false

解决方案

From what I can see, you cannot set a List View Item to be not selectable.

Other things you can do;

a) Add an event handler for the ItemSelectionChanged and handle it there
b) Use checkboxes and add an event handler for the ItemCheck/ItemChecked event

Kind Regards


Here is a very simple solution using the SelectedIndexChanged event handler for the ListBox control.

I'm checking to make sure the index is greater than -1 as that is an invalid index (-1 means nothing is selected). You can modify this as needed but as it stands the snippet makes it so any listbox item clicked on with the value "I can't be selected" will then clear all items selected. Use this event handler as you wish.
Hint: Change the backcolor of items :p

Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
    If ListBox1.SelectedIndex > -1 And
        ListBox1.Items(ListBox1.SelectedIndex) = "I can't be selected" Then
        ListBox1.ClearSelected()
    End If
End Sub


Here is another example to disable a listview item using combination of checkbox and ItemCheck Event. There also explanation on how to grey out the item.

c# - How do you disable an item in listview control in .net 3.5 - Stack Overflow[^]


这篇关于灰显或阻止列表视图项目选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆