我想要这个动态类
class FooViewModel : INotifyPropertyChanged {
private Dictionary<string, object> data = new Dictionary<string, object>();
public void Set(string path, object value)
{
data[path]=value;
NotifyPropertyChanged(path);
}
public object Get(string path)
{
if(data.ContainsKey(path)){
return data[path];
}else{
return null;
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged( String propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
如果我将其设置为某些 WPF XAML 的 DataContext,我希望能够将数据绑定到two way bind
其中的键。
<TextBox Text="{Bind Path=Foo}"/>
<TextBox Text="{Bind Path=Bar}"/>
在我的视图模型代码的某个地方,我将拥有
var vm = new FooViewModel();
vm.Set("Foo", "Yeah!");
vm.Set("Bar", "Booh!");
像这样进行自定义绑定有什么技巧?