【问题标题】:How do I bind to two different dependency properties in a ComboBox at the same time?如何同时绑定到 ComboBox 中的两个不同的依赖属性?
【发布时间】:2012-08-10 16:27:03
【问题描述】:

我有一个组合框,我已经创建了一个到项目列表的绑定,但是当我尝试绑定选定的项目属性时,它什么也没做。当我只绑定 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

【问题讨论】:

  • 你可以试试这样的,看看是否可行 将 DrowdownStyle 设置为 Dropdown 并绑定到 ComboBox 的 Text 属性而不是 SelectedItem。
  • createFieldBinding 方法的名称让我很困扰。您只能绑定到属性,而不是字段。 myField 是字段还是属性?
  • 您是否有理由不在 XAML 中以声明方式执行此操作?
  • @LukeWoodward 抱歉措辞,它是一个属性。我会改变它,这样它就不会那么混乱了。
  • @Charleh 我没有在 XAML 中声明它,因为这些字段是动态创建的。根据用户选择的表单类型,所有这些都绑定到不同的东西。即 Form A = 1 ComboBox 绑定到动物列表和文本框绑定到选定动物,Form B = 2 ComboBoxes 绑定到汽车品牌、汽车型号(分别)和汽车名称的文本框等。

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


【解决方案1】:
    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
}

只能绑定属性。字段不能绑定。希望对你有帮助。

【讨论】:

    【解决方案2】:

    它没有绑定的原因是因为我直接在 ViewModel 中定义字典,而不是创建一个临时字典并将其设置为实现 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;
     }
    

    【讨论】:

      猜你喜欢
      • 2012-11-11
      • 2019-04-30
      • 1970-01-01
      • 2018-12-09
      • 1970-01-01
      • 2011-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多