如何同时绑定ComboBox中的两个不同的依赖项属性?

时间:2012-08-10 16:27:03

标签: c# wpf data-binding combobox dependency-properties

我有一个ComboBox,我已经创建了一个绑定到项目列表,但当我尝试绑定所选的项目属性时,它没有做任何事情。当我仅绑定SelectedValueProperty时,它曾经工作。该类已经实现了INotifyPropertyChanged。

 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;
 }

我设置了一个更改字典变量的函数,并且ComboBox中的值发生了变化,但我无法更改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

2 个答案:

答案 0 :(得分: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
}

您只能绑定属性。字段不能被绑定。我希望这会有所帮助。

答案 1 :(得分:0)

它没有绑定的原因是因为我直接在ViewModel中定义了Dictionary而不是创建临时Dictionary并将其设置为实现INotifyPropertyChanged的Property,这会阻止Binding识别成员和一个领域必然属性。

而不是:

 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;
 }