如何正确添加PropertyPath?
我需要将usercontrol DataContext绑定到DataContext,TwoWay。
var binding = new Binding()
{
Mode = BindingMode.TwoWay,
Source = ((FrameworkElement)sender),
Path = new PropertyPath(FrameworkElement.DataContextProperty)
};
binding.Source = ((FrameworkElement) sender);
changeImage.SetBinding(FrameworkElement.DataContextProperty, binding);
答案 0 :(得分:3)
binding.Path = new PropertyPath("DataContext")
如果您说出代码有什么问题,也会有所帮助。
答案 1 :(得分:1)
PropertyPath
有一个字符串构造函数,它采用如下属性路径:
var binding = new Binding()
{
Mode = BindingMode.TwoWay,
Source = ((FrameworkElement)sender),
Path = new PropertyPath("DataContext")
};
或者,Binding
有一个构造函数,它将从give字符串参数创建PropertyPath
:
var binding = new Binding("DataContext")
{
Mode = BindingMode.TwoWay,
Source = ((FrameworkElement)sender)
};