2017-06-14 17 views
0

我應該補充說,這些對象是在第三方管理的.dll,所以我實際上沒有在我的代碼中的對象定義。如何將組合框的Display成員設置爲嵌套在對象列表中的字符串?

我有一個List<Foo>,我將其設置爲ComboBox的數據源。

假設Foo物體看起來像:這裏

public class Foo 
{ 
    Settings setting; 
    public Foo(string title) 
    { 
     setting = new Settings(); 
     setting.Title = title; 
    } 
    //lots of other objects and values 
} 

public class Settings 
{ 
    public Settings() 
    { 
    } 
    public String Title {get; set;} 
    //lot of other information 
} 

,我的實:

private void LoadList() //edited for SO testing sake, this is called in constructor 
         //of the form 
{ 
    var foo = new List<Foo> { 
     new Foo("MASTER 5DAY"), 
     new Foo("5 Day Reminder"), 
     new Foo("MASTER Welcome"), 
     new Foo("Welcome Letter") 
    }; 

    var master = foo.Where(x => x.Settings.Title.Contains("MASTER")).ToList(); 
    this.ddlMasterType = master; //This is the combobox 

    //Now here I would want the display member of the ddl to be the title of each Foo object 
    //But I need the value member to still contain all the other information of the Foo object 
} 

正如我在實施意見中列出,我想顯示Foo.Settings。標題,當我打開下拉列表,但我仍然需要完整的對象,當我稍後選擇它。

+0

這是winforms嗎? WPF? UWP? ASP.NET? –

+1

Woops我不好,它是winforms。 – Adam

回答

2

你在一個一點鹹菜。如果你設置了comboBox1.DataSource = foo;,通常你會設置comboBox1.DisplayMember = "SomePropertyName";,你會很好。但在你的情況下,你需要一個路徑,而不是名稱,comboBox1.DisplayMember = "settings.Title";將被忽略。

如果你可以改變Foo,你只是一個只讀的包裝屬性添加到Foo

public String Title { get { return settings.Title; } } 

但你不能改變Foo

所以這裏有一個選項:寫一個你可以改變的包裝類。

public class FooCarrier 
{ 
    public Foo Foo { get; set; } 
    public String Title { get { return Foo.settings.Title; } } 
} 

...

public Form1() 
{ 
    InitializeComponent(); 

    comboBox1.DataSource = new List<Foo> { 
     new Foo("MASTER 5DAY"), 
     new Foo("5 Day Reminder"), 
     new Foo("MASTER Welcome"), 
     new Foo("Welcome Letter") 
    }.Select(f => new FooCarrier { Foo = f }).ToList(); 

    comboBox1.DisplayMember = "Title"; 
    comboBox1.ValueMember = "Foo"; 

    comboBox1.SelectedIndexChanged += ComboBox1_SelectedIndexChanged; 
} 

private void ComboBox1_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    // comboBox1.SelectedItem will be FooCarrier, but since we set 
    // ValueMember to "Foo", SelectedValue is the Foo property of the selected 
    // item. 
    Foo selectedFoo = (Foo)comboBox1.SelectedValue; 
} 

你也可以省略FooCarrier和使用匿名類型:

comboBox1.DataSource = new List<Foo> { 
    new Foo("MASTER 5DAY"), 
    new Foo("5 Day Reminder"), 
    new Foo("MASTER Welcome"), 
    new Foo("Welcome Letter") 
}.Select(f => new { Foo = f, Title = f.Settings.Title }).ToList(); 

SelectedValue仍然是Foo,所以你永遠不會需要轉換到實際項目類型。

+0

工作就像一個魅力。包裝我的包裝類,誰知道!? – Adam

+0

我聽說你喜歡包裝類。 –

0

您可以將comboBox的數據上下文設置爲整個列表,並覆蓋Foo類的ToString方法以準確顯示您想要的內容。

public override string ToString() 
    { 
     return /*information you want to display, for example*/setting.Title; 
    } 
+0

我沒有訪問'Foo'類。 – Adam

0

嘗試將DisplayMember設置爲標題屬性:

master.DataSource = fooList; 
    master.DisplayMember = "Name" 

通過獲取整個對象:

Foo foo = (Foo)master.SelectedItem; 

Foo foo = master.SelectedItem as Foo; 
相關問題