我在我的DAL中使用SubSonic 2.2,并使用一个计算属性扩展了我的一个类,该属性返回一个字符串,该字符串包含另一个属性,该属性根据项目出现的轮廓级别缩进。属性的代码如下所示。问题是,当我试图使用这个属性作为一个窗体上的ListBox控件的DisplayMember(我在第一个地方写它的原因),它不会工作。ListBox恢复显示ID属性,该属性被设置为ValueMember。为了测试该属性是否正常工作,我循环遍历了填充ListBox的对象集合,并使用MessageBox.Show(obj.property)确认它确实返回了我正在寻找的值。我错过了什么,还是应该这样做?顺便说一句-可能有一个更好的方法来做缩进,但这不是我现在要的,谢谢!
代码:public partial class interorcategory: ActiveRecord, IActiveRecord{公共字符串{得到{
for (int i = 1; i < this.SpecLevel; i++) { returnValue += " "; } returnValue += this.CategoryName; return returnValue; } } }
<>
I definitely get data in my collection and the binding I'm doing is exactly the same as yours (binding code posted below). The return value of the ListDisplay property that I'm using is a string concatenation of two values in the object. Think of it as a "full name" property that concatenates the FirstName a space and the LastName properties into a single string which it returns. I am trying to bind the ListDisplay property to the DisplayMember property of the listbox, but all that shows in the listbox is the Id field which I am binding to the ValueMember.
private void FillCategories() { lstPackageCategories.DataSource = new InteriorsCategoryCollection().Load(); lstPackageCategories.DisplayMember = "CategoryName"; lstPackageCategories.ValueMember = "Id";
((InteriorsCategoryCollection)(lstPackageCategories.DataSource)).Sort("SpecSection", true);
lstPackageCategories.SelectedItem = lstPackageCategories.Items[0];currentCategory = (InteriorsCategory)lstPackageCategories.SelectedItem; RefreshAvailableItems(); }
如果您能够在集合中看到您的数据,那么听起来您的ListBox的绑定有问题。下面是我如何使用一个SubSonic值集合绑定ListBox的例子。
ISOCountryCodeCollection countrys =
new ISOCountryCodeCollection().OrderByAsc(ISOCountryCode.Columns.Country).Load();
Country.DataSource = countrys;
Country.DataValueField = "ThreeChar";
Country.DataTextField = "Country";
Country.DataBind();
在上面的例子中,我将3个字符的国家代码绑定到"DataValueField",并将完整的国家名称绑定到"DataTextField"。