Listbox.DisplayMember不会显示计算属性的值

时间:2011-04-21 14:36:16

标签: c# listbox subsonic2.2 calculated-field

我正在为我的DAL使用SubSonic 2.2并使用计算属性扩展我的一个类,该属性返回包含另一个属性的字符串,该属性具有基于项目发生的轮廓级别的缩进。该物业的代码如下。问题是,当我尝试将此属性用作表单上ListBox控件的DisplayMember(我首先编写它的原因)时,它将无法工作。 ListBox恢复为显示ID属性,该属性设置为ValueMember。为了测试该属性是否正常工作,我遍历了用List填充ListBox的对象集合,并使用MessageBox.Show(obj.property)确认它确实返回了我正在寻找的值。我错过了什么或应该这样做吗?顺便说一句 - 可能有更好的方法来进行缩进,但这不是我现在所追求的,谢谢!

代码如下:

public partial class InteriorsCategory : ActiveRecord, IActiveRecord { public string ListDisplay { get { string returnValue = "";

            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(); }
currentCategory = (InteriorsCategory)lstPackageCategories.SelectedItem; RefreshAvailableItems(); }

1 个答案:

答案 0 :(得分:0)

如果您能够在集合中看到您的数据,那么听起来您的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”。