【问题标题】:WPF Data Binding to Dictionary in data objectWPF数据绑定到数据对象中的字典
【发布时间】:2016-01-11 14:24:38
【问题描述】:

我有一个 USER 数据对象,其中包含这样的字典:

public class User : INotifyPropertyChanged
{
   private Dictionary<string, object> _metadata;

   public Dictionary<string, object> Metadata
    {
        get { return this._metadata; }
        set
        {
            this._metadata = value;                
            OnPropertyChanged("Metadata", value);
        }
    }
    protected void OnPropertyChanged(string property, object value)
    {           
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    }
}

我也有 ViewModel 由这样的用户数据对象组成:

public class ViewModel : INotifyPropertyChanged
{  
public static ViewModel getInstance()
        {
            if (instance == null)
                instance = new ViewModel();
            return instance;
        }
private User _user;

public User User
        {
            get { return this._user; }
            set
            {
                this._user = value;
                OnPropertyChanged("User", value);
            }
        }
protected void OnPropertyChanged(string property, object value)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }
}  

然后在我的主窗口中,我在文本框和字典值(不是字典键 ....)之间进行数据绑定。

Binding myBinding = new Binding();
myBinding.Source = ViewModel.getInstance().User.Metadata;
myBinding.Path = new PropertyPath("SomeDictonaryKey");
myBinding.Mode = BindingMode.TwoWay;
myBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
BindingOperations.SetBinding(tb1, TextBox.TextProperty, myBinding));

但它不起作用,我尝试了不同的方法来更改 binding.pathbinding.source,它仍然不起作用....

谁能告诉我这是什么问题???

【问题讨论】:

    标签: c# wpf dictionary data-binding


    【解决方案1】:

    您可以通过标准索引器语法绑定到字典值:

    Binding myBinding = new Binding();
    myBinding.Source = ViewModel.getInstance().User;
    myBinding.Path = new PropertyPath("Metadata[SomeDictonaryKey]");
    myBinding.Mode = BindingMode.TwoWay;
    myBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
    BindingOperations.SetBinding(tb1, TextBox.TextProperty, myBinding));
    

    或者您可以使用 source 指定绑定源的所有路径:

    myBinding.Source = ViewModel.getInstance().User.Metadata[SomeDictonaryKey];
    myBinding.Path = new PropertyPath(".");
    

    您可以找到更多示例here

    【讨论】:

    • 太棒了!但是当我更改文本框值时该值没有改变.....似乎 Onpropertychange 不起作用??
    • 应该可以。您可以查看更多示例here。您如何检查更改?您在视图模型中在哪里实例化用户?当您检查更改时,您确定您有相同的对象吗?你保存更改吗?
    • 我尝试将两个文本框绑定到同一个字典键......它不起作用......正确,当我改变另一个文本框的值时,文本框应该改变值。 ...
    • 不应该,因为您的字典索引器没有实现INotifyPropertyChanged。对于这种情况,您应该使用 ObservableDictionary blogs.microsoft.co.il/shimmy/2010/12/26/…
    • 您是否实际测试过第二个建议中的代码?我很确定在没有指定PathXPath 的情况下设置双向绑定将导致InvalidOperationException...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-10-15
    • 1970-01-01
    • 1970-01-01
    • 2010-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多