如何正确设置PropertyPath

时间:2011-06-07 18:46:10

标签: c# silverlight-4.0 binding

如何正确添加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);

2 个答案:

答案 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)
                };