问题描述
我有一个 System.Windows.Forms.CheckedListBox
的实例,它显示了一个复选框列表,我的应用程序中有一些其他的 System.Windows.Forms
对象.我想,根据用户使用其他 System.Windows.Forms
项目选择的内容,显示或隐藏 System.Windows.Forms 中的不同项目
.CheckedListBox.我怎么能做到这一点?
I have an instance of System.Windows.Forms.CheckedListBox
which displays a list of tick boxes and I have some other System.Windows.Forms
objects in my application. I would like to, depending on what the user selects using the other System.Windows.Forms
items, show or hide different items in the System.Windows.Form.CheckedListBox
. How could I achieve this?
注意:这是针对 Windows 桌面应用程序,而不是网页.
推荐答案
没有简单的方法可以隐藏 CheckedListBox
中的项目,您必须将其删除,就像 Brendan Vogt 向您展示的那样.
There is no easy way to hide an item in a CheckedListBox
, you have to remove it, like Brendan Vogt showed you.
另一种方法是利用数据绑定.不应该为 CheckedListBox
工作,DataSource
属性的文档说:
An alternative is to take advantage of data binding. It is not supposed to work for CheckedListBox
, the documentation of the DataSource
property says:
此 API 支持 .NET Framework 基础结构,不能直接从您的代码中使用.
获取或设置控件的数据源.此属性与此类无关.
不过我以前用过,效果很好.因此,如果您将 DataView
指定为列表的 DataSource
,则可以使用 RowFilter
属性
However, I used it in the past, and it works fine. So if you assign a DataView
as the DataSource
for the list, you can filter its items using the RowFilter
property
DataView view = new DataView(productsDataTable);
checkedListBox.DataSource = view;
checkedListBox.DisplayMember = "Name";
...
// Hide discontinued products
view.RowFilter = "Discontinued = False";
这篇关于如何隐藏/显示 CheckedListBox 中的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!