簡體   English   中英

使用對象集合設置列表框項目文本

[英]set listbox items text using collection of objects

我需要能夠將列表框控件項文本設置為通用對象的屬性。

foreach ( GenericObject obj in _genericObjectsCollection )
{
   listbox1.Items.Add( obj );
   listbox1.Items.Text = obj.ItemsText;
}

通用對象類

public class GenericObject
{
   public string ItemsText { get; set; }
   public OtherClass TheOtherGenericClass { get; set; }
}

問題

文本呈現為“ myProject.GenericObject”

它沒有獲取text屬性,而是簡單地呈現對象類型。


我需要什么

文本呈現為:

“通用對象1的文本”

“通用對象2的文本”

對於集合中的每個對象,依此類推。


最后的話

我知道使用適當的數據源時,可以使用列表框控件的display member屬性,但是將其與集合作為​​數據源一起使用時會出現異常。 它告訴我不能使用復雜的類來完成此任務。


回答問題

使用數據源屬性時會發生什么?

我使用的代碼

    foreach ( GenericObject obj in _genericObjectsCollection  )
    {
        listboxProcessedQuotes.DataSource = obj ;
        listboxProcessedQuotes.DisplayMember = obj.ItemsText;
    }

結果

在此處輸入圖片說明

foreach ( GenericObject obj in _genericObjectsCollection )
{
   listbox1.Items.Add( obj.itemsText );
}

如果還需要獲取基礎的選定對象,則可以將其存儲在Tag屬性中:

foreach ( GenericObject obj in _genericObjectsCollection )
{
   ListBoxItem itm = new ListBoxItem();
   itm.Content = obj.itemsTxt;
   itm.Tag = obj;
   listbox1.Items.Add(itm);
}

然后,當您獲得選定的列表框項目時,可以執行以下操作:

itm.Tag as GenericObject

我很好奇您如何嘗試為列表框執行顯示成員路徑。 確實應該有可能。 這是一個簡單的例子:

//xaml
<ListBox x:Name="ListBox" DisplayMemberPath="MyStringProperty"/>

//code behind
 public partial class MainWindow : Window
 {
    public MainWindow()
    {
        InitializeComponent();
        this.LoadItems();
    }

    private void LoadItems()
    {
        ListBox.Items.Add(new MyListItem() { MyBoolProperty = true, MyStringProperty = "Hello" });
        ListBox.Items.Add(new MyListItem() { MyBoolProperty = false, MyStringProperty = "World" });
    }
}

//my object class
public class MyListItem : NotificationObject
{
    private string _myString;
    private bool _myBool;

    public MyListItem()
    { }

    public string MyStringProperty
    {
        get { return _myString; }
        set
        {
            _myString = value;
            this.RaisePropertyChanged("MyStringProperty");
        }
    }

    public bool MyBoolProperty
    {
        get { return _myBool; }
        set
        {
            _myBool = value;
            this.RaisePropertyChanged("MyBoolProperty");
        }
    }
}

這是您的GenericObject應該是這樣的:

public class GenericObject
{
    private string _itemsText;
    public string itemsText
    {
        get { return _itemsText; }
        set { _itemsText = value; }
    }
}

然后創建您的自定義對象的簡單列表,並將其添加到ListBoxItemsSource中:

List<GenericObject> l = new List<GenericObject>();
l.Add(new GenericObject() { itemsText = "test1" });
l.Add(new GenericObject() { itemsText = "test2" });

list1.ItemsSource = l;

注意ItemTemplate的XAML聲明,我正在使用綁定:

<ListBox x:Name="list1">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding itemsText}" />
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

我建議閱讀更多有關數據綁定的信息,這非常有用。 https://msdn.microsoft.com/zh-CN/library/ms750612%28v=vs.110%29.aspx

暫無
暫無

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

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