设置PropertyPath的正确性如何

本文关键字:正确性 PropertyPath 设置 | 更新日期: 2023-09-27 17:58:48

如何正确添加PropertyPath?

我需要将用户控制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);

设置PropertyPath的正确性如何

binding.Path = new PropertyPath("DataContext")

如果你能说出你的代码出了什么问题,这也会有所帮助。

PropertyPath有一个字符串构造函数,它采用如下属性路径:

  var binding = new Binding()
                {
                   Mode = BindingMode.TwoWay,
                   Source = ((FrameworkElement)sender),
                   Path = new PropertyPath("DataContext")
                };

或者,Binding有一个构造函数,它将根据给定的字符串参数创建PropertyPath

  var binding = new Binding("DataContext")
                {
                   Mode = BindingMode.TwoWay,
                   Source = ((FrameworkElement)sender)
                };