【问题标题】:WPF Binding in a binding Dictionary绑定字典中的 WPF 绑定
【发布时间】:2014-03-31 19:21:57
【问题描述】:

我正在尝试访问一个复杂对象的字符串属性,该属性是我的 Dictionary 的值。关键是我班级的一个属性。

<DataGridTextColumn Binding="{Binding Path=MyDictionary[{Binding MyClassProp}].MyObjValue}"/>

绑定中的绑定不起作用,因为它不是有效的 XAML。有没有其他方法可以做到这一点?

【问题讨论】:

    标签: c# wpf


    【解决方案1】:

    是的,双重Binding 将不起作用。我无法对此进行测试,但也许你可以这样做:

    让你的班级实现INotifyPropertyChanged:

    public event PropertyChangedEventHandler PropertyChanged;
    
    protected void Notify(string propName)
    {
        if (this.PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propName));
    }
    

    当您的MyClassProp 属性发生变化时:

    private string _MyClassProp;
    public string MyClassProp
    {
        get { return _MyClassProp; }
        set
        {
            _MyClassProp = value;
    
            MyDictValue = MyDictionary[MyClassProp].MyObjValue;
            Notify("MyClassProp");
        }
    }
    

    请注意,当MyClassProp 更改时,您使用字典的值设置了另一个属性。然后定义该属性以用作绑定:

    private string _MyDictValue;
    public string MyDictValue
    {
        get { return _MyDictValue; }
        set
        {
            _MyDictValue = value;
            Notify("MyDictValue");
        }
    }
    

    然后将您的 xaml 绑定更改为:

    <DataGridTextColumn Binding="{Binding MyDictValue}"/>
    

    【讨论】:

      猜你喜欢
      • 2011-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-20
      • 2012-09-26
      • 2023-04-01
      • 2011-03-27
      相关资源
      最近更新 更多