2016-03-30 31 views
0

我有三個ComboBox。 Threre綁定的是相同的ItemsSource。我怎樣綁定很多ComboBox的ItemsSoure互相連接

這ItemsSouce類型是Dictionary<string, Dictionary<CustomKey, CustomClass>>

CustomKey

public struct CustomKey<T1,T2> // T1, T2 is string 
{ 
    public readonly T1 Symbol; 
    public readonly T2 Column; 
    public CustomKey(T1 key1, T2 key2) { Symbol = key1; Column = key2; } 
} 

CustomClass

public class CustomClass 
{ 
    public string Value{get;set} 
} 

首先。 FirstComboBox的ItemsSoure是Dictionary.Keys

二。我想設置第二個組合框的項目源是FirstComboBox的SelectedItems。
這樣字典[FirstComboBox.SelectedItem] .Keys T1

第三。 ThirdComboBox的的ItemsSource 字典[FirstComboBox.SelectedItem] .Keys T2

最後。
這是我的代碼...
// Source.GetNames is ItemsSource.Keys.ToList();

protected virtual FrameworkElement CreateAutoCompleteComboBoxControl(PropertyItem property) 
    { 
     var c = new AutoCompleteComboBox(); 
     ContinuityBindablePropertyItem cbp = (property as ContinuityBindablePropertyItem); // cbp is First ComboBox SelectedItem Descriptor. but i don't know how to use... 

     if (property.DisplayName == "Sheet") // First ComboBox 
     { 
      c.SetBinding(ItemsControl.ItemsSourceProperty, new Binding("Source.GetSheetNames"); 
     } 
     else if (property.DisplayName == "TestName") // Second ComboBox 
     { 
      c.SetBinding(ItemsControl.ItemsSourceProperty, new Binding("Source.GetSheetNames"); 
     } 
     else if (property.DisplayName == "Symbol") // Third ComboBox 
     { 
      c.SetBinding(ItemsControl.ItemsSourceProperty, new Binding("Source.GetSheetNames"); 
     } 

     c.SetBinding(ComboBox.TextProperty, property.CreateBinding()); 
     return c; 
    } 

請幫我。 我不會說英語。我希望你明白。

謝謝。


編輯 改變方法。

protected virtual FrameworkElement CreateAutoCompleteComboBoxControl(PropertyItem property) 
    { 
     StackPanel s = new StackPanel(); 
     var c1 = new AutoCompleteComboBox { Name = "c1", DisplayMemberPath = "Key", IsEditable = property.IsEditable, ItemsSource = property.ItemsSource, VerticalContentAlignment = VerticalAlignment.Center }; 
     var c2 = new AutoCompleteComboBox { IsEditable = property.IsEditable, ItemsSource = property.ItemsSource, VerticalContentAlignment = VerticalAlignment.Center }; 
     var c3 = new AutoCompleteComboBox { IsEditable = property.IsEditable, ItemsSource = property.ItemsSource, VerticalContentAlignment = VerticalAlignment.Center }; 

     c1.SetBinding(ItemsControl.ItemsSourceProperty, new Binding("Source") { Source = MappingService.Instance, Mode = BindingMode.OneWay }); 
     c2.SetBinding(ItemsControl.ItemsSourceProperty, new Binding("Values") { Source = c1.SelectedItem, Mode = BindingMode.OneWay }); 
     c3.SetBinding(ItemsControl.ItemsSourceProperty, new Binding("SelectedItem.Values") { ElementName = c1.Name, Mode = BindingMode.OneWay }); 

     c1.SetBinding(ComboBox.TextProperty, property.CreateBinding()); 
     c2.SetBinding(ComboBox.TextProperty, property.CreateBinding()); 
     c3.SetBinding(ComboBox.TextProperty, property.CreateBinding()); 

     s.Children.Add(c1); 
     s.Children.Add(c2); 
     s.Children.Add(c3); 
     return s; 
    } 

我嘗試綁定路徑 '價值', '價值', 'SelectedItem.Value',「SelectedItem.Values,輸入源切換至C1.SelectedItem或元素名稱= C1 ...

但它的不工作

編輯
添加圖像。 enter image description here

+0

你有任何xaml,或者你在做代碼中的一切嗎? – Joe

+0

@joe是的,我不用你的xaml。僅在 – user3214694

+0

後面的代碼什麼是您的第一個組合框的itemSource。你能添加完整的結構嗎?這不是很清楚第二。我想設置Second ComboBox的Items Source是FirstComboBox的SelectedItems。 今日語言字典[FirstComboBox.SelectedItem] .Keys T1 三。 ThirdComboBox的ItemsSource字典[FirstComboBox.SelectedItem] .Keys T2 – Nitin

回答

1

我對你想要做的事情感到有點困惑,我想我需要更多的代碼或者一個例子來讓我的頭腦得到解決。但我會嘗試解決一些問題。另外,我習慣了xaml,所以你可能需要填寫綁定代碼的空白:

首先, FirstComboBox的ItemsSoure是Dictionary.Keys

只是爲了檢查,這是工作?

二。我想設置Second ComboBox的Items Source是 FirstComboBox的SelectedItems。像這樣 字典[FirstComboBox.SelectedItem] .Keys T1

三。 ThirdComboBox的的ItemsSource 詞典[FirstComboBox.SelectedItem] .Keys T2

這兩個本質上是同樣的問題,你要綁定到Dictionary.Keys,但有一個組合框顯示T1和T2等?好吧,我不建議直接將ComboBox綁定到Dictionary [FirstComboBox.SelectedItem] .Keys T1,而是建議使用兩種方法之一。

1.使用多重綁定轉換器。您的第一個複選框的ItemsSource將被綁定到一個字符串列表。這是它的SelectedItem將是一個字符串,是字典>的關鍵。

可以同時通過鍵和字典的multibinding轉換器和具有它返回字典的鍵列表或數組是這樣的:

public class MyDictionaryValueConverter : IMultiValueConverter 
{ 
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     //first item is our key 
     string key = values[0] as string; 

     //second item is our dictionary 
     Dictionary<string, Dictionary<CustomKey, CustomClass>> dictionary = values[1] as Dictionary<string, Dictionary<CustomKey, CustomClass>>; 

     //pass our value to be bound to 
     return dictionary[key].Keys.ToList(); 
    } 
} 

你知道如何設置轉換器的綁定代碼?可能需要研究多重綁定,我只能在xaml中完成它。

2.你也可以做到這一點使用你不必直接綁定到中間的一切屬性,你可以創建你的數據對象的屬性是這樣的:

public class DataContextThatContainsYourDictionary : INotifyPropertyChanged 
{ 
    //notifying property that is bound to ItemsSource in the first Combobox 
    public Dictionary<string, Dictionary<CustomKey, CustomClass>> MyDictionary { get... } 

    //This is the string that's bound to SelectedItem in the first ComboBox 
    public string SelectedKey 
    { 
     get 
     { 
      return selectedKey; 
     } 
     set 
     { 
      //standard notify like all your other bound properties 
      if (selectedKey != value) 
      { 
       selectedKey = value; 
       //when this changes, our selection has changed, so update the second list's ItemsSource 
       SelectedKeys = MyDictionary[SelectedKey].Keys.ToList(); 
       NotifyPropertyChanged("SelectedKey"); 
      } 
     } 
    } 

    //an "intermediatary" Property that's bound to the second Combobox, changes with the first's selection 
    public List<CustomKey> SelectedKeys { get ... } 

這些都具有相同的結果,你將最終將你的ComboBoxes綁定到List。然後,您可以將第一個複選框的DisplayMemberPath設置爲T1,或者將第二個的T2設置爲DisplayMemberPath。

+0

添加圖片以幫助我的問題 – user3214694

+0

是的,首先comboBox正在工作。但是,第二個和第三個組合框不起作用。 – user3214694

+0

和...'@joe'<< - 不工作.. – user3214694

1

嘗試設置如下的綁定:您可能還需要爲其他兩個組合框設置DisplayMemberPath,具體取決於您希望在其中顯示哪個屬性。

var c1 = new AutoCompleteComboBox { Name = "c1", DisplayMemberPath = "Key", IsEditable = property.IsEditable, VerticalContentAlignment = VerticalAlignment.Center }; 
var c2 = new AutoCompleteComboBox { IsEditable = property.IsEditable, VerticalContentAlignment = VerticalAlignment.Center }; 
var c3 = new AutoCompleteComboBox { IsEditable = property.IsEditable, VerticalContentAlignment = VerticalAlignment.Center }; 

c1.SetBinding(ItemsControl.ItemsSourceProperty, new Binding("Source") { Source = MappingService.Instance, Mode = BindingMode.OneWay }); 
c2.SetBinding(ItemsControl.ItemsSourceProperty, new Binding() { Path = new PropertyPath("SelectedItem.Value.Keys"), ElementName = c1.Name, Mode = BindingMode.OneWay }); 
c3.SetBinding(ItemsControl.ItemsSourceProperty, new Binding() { Path = new PropertyPath("SelectedItem.Value.Values"), ElementName = c1.Name, Mode = BindingMode.OneWay });