0

I'm using SubSonic 2.2 for my DAL and extended one of my classes with a calculated property that returns a string containing another property with indenting based on the level of the outline at which the item occurs. The code for the property is below. The problem is that when I try to use this property as the DisplayMember for a ListBox control on a form (the reason I wrote it in the first place) it won't work. The ListBox reverts to displaying the ID property which is set as the ValueMember. To test that the property was working I looped through the collection of objects that I was populating the ListBox with and, using MessageBox.Show(obj.property), confirmed that it was indeed returning the value I'm looking for. Am I missing something or should this work? btw - There may be a better way to do the indenting but that's not what I'm after at the moment, thanks!

Code follows:

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(); }

4

1 回答 1

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”。

于 2011-04-27T16:53:15.647 回答