簡體   English   中英

Listbox.DisplayMember不會顯示計算所得屬性的值

[英]Listbox.DisplayMember won't display value from calculated property

我將SubSonic 2.2用於DAL,並使用計算所得的屬性擴展了我的一個類,該屬性返回一個字符串,其中包含另一個屬性,該字符串根據項目出現的輪廓級別縮進。 該屬性的代碼如下。 問題是,當我嘗試使用此屬性作為窗體上的ListBox控件的DisplayMember時(首先我寫它的原因),它將無法正常工作。 列表框還原為顯示ID屬性,該屬性設置為ValueMember。 為了測試該屬性是否正常運行,我遍歷了我使用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(); }

如果您能夠看到集合中的數據,則聽起來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”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM