【发布时间】: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.path 和 binding.source,它仍然不起作用....
谁能告诉我这是什么问题???
【问题讨论】:
标签: c# wpf dictionary data-binding