2012-08-10 13 views
0

我有了我創建了一個結合項目的列表組合框,但是當我嘗試將所選項目屬性綁定,它不會做任何事情。它只用於綁定SelectedValueProperty時用於工作。該類已經實現了INotifyPropertyChanged。如何綁定到一個ComboBox兩種不同的依賴屬性在同一時間?

public void ComboBoxBinding() { 
     Dictionary<long, string> myDictionary = new Dictionary<long, string> 
     Control control = new ComboBox(); 
     comboBoxControl = (ComboBox)control; 
     comboBoxControl.SetBinding(ComboBox.ItemsSourceProperty, createFieldBinding("myDictionary")); 
     comboBoxControl.DisplayMemberPath = "Value"; 
     comboBoxControl.SelectedValuePath = "Key"; 
     binding = createFieldBinding(fieldProperty); 
     control.SetBinding(ComboBox.SelectedItemProperty, createFieldBinding("fieldProperty")); // <-- This doesn't seem to bind. 
} 

private Binding createFieldBinding(string propertyName) { 
     Binding binding = new Binding(fieldName); 
     binding.Source = this.DataContext; 
     binding.UpdateSourceTrigger = System.Windows.Data.UpdateSourceTrigger.PropertyChanged; 

return binding; 
} 

我建立,將改變字典的變量,並在組合框的值改變的功能,但我不能讓SelectedValueProperty改變。我怎麼做?

編輯:如果我創建了一個字典和手動設置的ItemsSource,它的工作原理,但是當我設置的結合,它沒有。

Dictionary<long, string> myDictionary = new Dictionary<long, string>(); 
myDictionary.Add(1, "test1"); 
myDictionary.Add(2, "test2"); 
myDictionary.Add(3, "test3"); 
myDictionary.Add(4, "test4"); 
myDictionary.Add(5, "test5"); 
myDictionary.Add(6, "test6"); 
myDictionary.Add(7, "test7"); 
myDictionary.Add(8, "test8"); 
myDictionary.Add(9, "test9"); 
myDictionary.Add(10, "test10"); 
comboBoxControl.ItemsSource = myDictionary; //<-- This works, but if I use Binding instead of manually setting the ItemsSource, it does not work 
+0

你可以嘗試這樣的事情,看看它是否工作 設置DrowdownStyle到下拉列表並綁定到ComboBox的Text屬性代替的SelectedItem。 – MethodMan 2012-08-10 16:37:48

+0

你'createFieldBinding'方法的名稱困擾我。您只能綁定到屬性,而不是字段。 'myField'是一個字段還是一個屬性? – 2012-08-10 16:44:11

+0

有你沒有在XAML聲明這樣做的一個原因? – Charleh 2012-08-10 16:48:24

回答

0

它未結合的原因是因爲我被限定在視圖模型的字典,而不是直接創建臨時字典,並將其設置到該實現INotifyPropertyChanged,屬性,其停止從識別部件之間的鏈路的結合和一個領域必然的屬性。

而不是做的:

private Dictionary<long, string> _myList = new Dictionary<long, string>(); 
public Dictionary<long, string> MyList { 
     get { return _myList; } 
     set { _myList = value; 
      PropertyChanged("MyList"); } 
} 

public void Init() { 
     _myList.Add(1, "One"); 
} 

我必須設置一個臨時的字典並應用到屬性。

private Dictionary<long, string> _myList = new Dictionary<long, string>(); 
public Dictionary<long, string> MyList { 
     get { return _myList; } 
     set { _myList = value; 
      PropertyChanged("MyList"); } 
} 

public void Init() { 
     Dictionary<long, string> tempList = new Dictionary<long, string>(); 
     tempList.Add(1, "One"); 
     MyList = tempList; 
} 
0
public partial class MainWindow : Window, INotifyPropertyChanged 
{ 
    public long SelectedValue { get { return selectedValue; } set { selectedValue = value; Notify("SelectedValue"); } } 
    private long selectedValue; 
    private Dictionary<long, string> myDictionary; 
    public Dictionary<long, string> MyDictionary { get { return myDictionary; } set { myDictionary = value; Notify("MyDictionary"); } } 
    public MainWindow() 
    { 
     InitializeComponent(); 
     DataContext = this; 
     ComboBoxBinding(); 
     MyDictionary = new Dictionary<long, string>() { { 1, "abc" }, { 2, "xyz" }, { 3, "pqr" } }; 
     SelectedValue = 2; 

    } 
    public void ComboBoxBinding() 
    { 
     Control control = new ComboBox(); 
     comboBoxControl = (ComboBox)control; 
     comboBoxControl.SetBinding(ComboBox.ItemsSourceProperty, createFieldBinding("MyDictionary")); 
     comboBoxControl.DisplayMemberPath = "Value"; 
     comboBoxControl.SelectedValuePath = "Key"; 
     comboBoxControl.SetBinding(ComboBox.SelectedValueProperty, createFieldBinding("SelectedValue")); 
    } 

    private Binding createFieldBinding(string fieldName) 
    { 
     Binding binding = new Binding(fieldName); 
     binding.Source = this.DataContext; 
     binding.UpdateSourceTrigger = System.Windows.Data.UpdateSourceTrigger.PropertyChanged; 
     return binding; 
    } 
    private void Notify(string propName) 
    { 
     if(PropertyChanged!=null) 
      PropertyChanged(this,new PropertyChangedEventArgs(propName)); 
    } 

    #region INotifyPropertyChanged Members 

    public event PropertyChangedEventHandler PropertyChanged; 

    #endregion 
} 

可以綁定屬性only.Fields不能Binded.I希望這將有助於。

相關問題