2016-08-15 66 views
0

我有一個ObservableCollection<M> fooBar {get;set;}。類M.cs看起來是這樣的:DropDown綁定到屬性名稱;基於第一套第二套DropDown綁定

public class M{ 
    private int _ID; 
    public int ID { 
     get {return this._ID;} 
     set {this._ID = value;} 
    } 

    private string _number; 
    public int Number { 
     get {return this._number;} 
     set {this._number = value;} 
    } 

    private string _power; 
    public int Power { 
     get {return this._power;} 
     set {this._power = value;} 
    } 

    /* 
     ... 
    */ 
} 

現在,我想這3個propertys的名字綁定到ComboBox。我不想這樣做:

<ComboBox> 
    <ComboBoxItem>ID</ComboBoxItem> 
    <ComboBoxItem>Number</ComboBoxItem> 
    <ComboBoxItem>Power</ComboBoxItem> 
</ComboBox> 

有沒有更舒適的方式?我想填寫第二個ComboBox。作爲例子,我在第一ComboBox選擇屬性Number那麼第二ComboBox應該是這樣的

<ComboBox 
    SelectedValue="{Binding ???}" 
    ItemsSource="{Binding fooBar}" 
    SelectedValuePath="Number" 
    DisplayMemberPath="Number" 
    /> 

也許你有人能幫助我,因爲我不知道如何既組合框連接。

回答

1

這是我會怎麼做:

  • 就可以選擇其中暴露了美國在模型(M級)的屬性視圖模型的屬性。這樣你可以明確地控制可以選擇哪些屬性。

  • 製作屬性以保存每個組合框的選定值。

  • ComboBox2中的DisplayMemberPath/SelectedValuePath綁定到ComboBox1的SelectedValue。

視圖模型:

 // ComboBox 1 
     public Dictionary<string, string> SelectableProperties = new Dictionary<string, string>() 
     { 
      { nameof (M.ID), "ID" } 
      { nameof (M.Number), "Nummer" } 
      { nameof (M.Power), "Potenz" } 
     } 

     // Selection in combobox 1 (not strictly necessary as it can be handled in view, but you may need to know what SelectedValue represents) 
     private string _selectedValueMember = String.Empty; 
     public string SelectedValueMember 
     { 
      get { return _selectedValueMember; } 
      set { _selectedValueMember = value; } 
     } 

     // Selection in combobox 2 (object just in case there could be other values than int) 
     private object _selectedValue = null; 
     public object SelectedValue 
     { 
      get { return _selectedValue; } 
      set { _selectedValue = value; } 
     } 

     public ObservableCollection<M> FooBar{ get; set; } 

查看:

<ComboBox x:Name="ComboBox1" 
        Width="100" 
        Margin="5" 
        SelectedValuePath="Key" 
        DisplayMemberPath="Value" 
        SelectedValue="{Binding SelectedValueMember}" 
        ItemsSource="{Binding SelectableProperties}"> 

     </ComboBox> 
     <ComboBox Width="100" 
        Margin="5" 
        DisplayMemberPath="{Binding ElementName=ComboBox1, Path=SelectedValue}" 
        SelectedValuePath="{Binding ElementName=ComboBox1, Path=SelectedValue}" 
        SelectedValue="{Binding SelectedValue}" 
        ItemsSource="{Binding FooBar}"> 

     </ComboBox> 
+0

感謝您的回答@sondergard!我有一個問題。我不想顯示所有的屬性,我想把它們翻譯成德文。我怎樣才能做到這一點? – MyNewName

+1

本地化可以使用字典完成,其中值部分是本地化文本,並且selectedvaluepath/displaymemberpath分別指向鍵/值。根據我對德語的回憶,我已經用最好的猜測更新了樣本(它已經,15年了?):)如果你願意,你當然也可以使用資源中的字符串。 – sondergard

+0

非常感謝!現在它可以工作。從未使用'字典'befor。 ^^ – MyNewName

1

對於第一個組合框:使用反射來獲取類M的所有屬性的名稱並將其放入組合框中。

對於第二個組合框:當第一個選擇更改時,您將獲得屬性名稱。現在將SelectedValue綁定設置爲第一個ComboBox中選定的屬性。