我有了我創建了一個結合項目的列表組合框,但是當我嘗試將所選項目屬性綁定,它不會做任何事情。它只用於綁定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
你可以嘗試這樣的事情,看看它是否工作 設置DrowdownStyle到下拉列表並綁定到ComboBox的Text屬性代替的SelectedItem。 – MethodMan 2012-08-10 16:37:48
你'createFieldBinding'方法的名稱困擾我。您只能綁定到屬性,而不是字段。 'myField'是一個字段還是一個屬性? – 2012-08-10 16:44:11
有你沒有在XAML聲明這樣做的一個原因? – Charleh 2012-08-10 16:48:24