将列表框绑定到绑定列表,对项目属性进行过滤 [英] Binding Listbox to bindinglist, filter on item property
问题描述
我正在使用Winforms应用程序,并且有一个要绑定到列表框的对象的绑定列表.我可以执行此操作,但是接下来要做的是仅显示具有特定属性的项.
I'm working on a Winforms application and I have a bindinglist of objects that I want to bind to a listbox. I got this to work, but what I want to do next, is only display items where a particular property is true.
所以我有一个带有绑定列表的类
So I have a class with a bindinglist
class DataBuilder
{
public BindingList<TableSet> allTableSets = new BindingList<TableSet>();
}
还有一个带有某些属性的类TableSet
And a class TableSet
with some properties
class TableSet
{
public string TableSetName {get; set;}
public bool IsPopulated {get; set;}
}
现在在我的表单上,我想将一个列表框绑定到allTableSet,但是只显示IsPopulated == true
And now on my form, I want to bind a listbox to the allTableSets, but only show the items where IsPopulated == true
到目前为止,我的表单上只显示了allTableSets列表中的所有项目
What I have so far on my form just shows all the items in the allTableSets list
public partial class MainForm : Form
{
DataBuilder dataBuilder = new DataBuilder();
{
this.populatedTableSetsListBox.DataSource = dataBuilder.allTableSets;
this.populatedTableSetsListBox.DisplayMember = "TableSetName";
}
}
我一直在网上浏览,但是没有发现任何与我尝试做的事情相似的事情.任何建议或替代方法都将不胜感激.谢谢
I've been looking around the web but haven't found anything that seems similar to what I"m trying to do. Any suggestions or alternate methods are greatly appreciated. Thank you
推荐答案
尝试一下:在您的DataBuilder
类中,有一个函数可以根据您的过滤条件返回项的子集.
Try this: in your DataBuilder
class, have a function that returns a subset of your items based on your filter condition.
例如,在您的DataBuilder
类中:
public BindingList<TableSet> someTableSets()
{
BindingList<TableSet> someTableList = new BindingList<TableSet>();
foreach (TableSet TS in allTableSets)
if (TS.IsPopulated == true)
someTableList.Add(TS);
return someTableList;
}
然后,在您的MainForm
中,将其设置为等于someTableSets()
函数的结果,而不是将DataSource
设置为allTableSets
:
Then, in your MainForm
, instead of setting the DataSource
to allTableSets
, set it equal to the result of the someTableSets()
function:
this.populatedTableSetsListBox.DataSource = dataBuilder.someTableSets();
这篇关于将列表框绑定到绑定列表,对项目属性进行过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!