Skip to main content
 首页 » 编程设计

c#之如何正确设置 PropertyPath

2024年02月23日19findumars

如何正确添加 PropertyPath ?

我需要将用户控件 DataContext 绑定(bind)到 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); 

请您参考如下方法:

binding.Path = new PropertyPath("DataContext") 

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