WPF - 绑定到明确实现的接口属性从代码背后/附加的行为 [英] WPF - binding to an explicitly implemented interface property from code behind/attached behaviour

查看:157
本文介绍了WPF - 绑定到明确实现的接口属性从代码背后/附加的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从代码隐藏设置一个明确实现的接口属性的绑定。代码隐藏绑定的原因是绑定属性的路径只能在运行时确定。

I am trying to set up a binding to an explicitly implemented interface property from code-behind. The reason for the code-behind binding is that the path to the bound property can only be determined at run-time.

在XAML中,可以绑定(MainWindow.xaml中的示例):

In the XAML, it is possible to bind thusly (example in MainWindow.xaml):

<TextBox Text="{Binding (local:IViewModel.Property)}"/>

事实上,代码背后的绑定以类似的方式工作(从MainWindow.xaml.cs ):

and in fact, binding in the code behind works in a similar way (from MainWindow.xaml.cs):

var binding = new Binding("(local:IViewModel.Property)");

因为WPF能够选择命名空间映射。

since WPF is able to pick up the namespace mapping.

我的问题是,当命名空间映射不存在时,如何形成这样的绑定(例如,附加行为)?

My question is, how do I form a binding like this when the namespace mapping is not present (for example, in an attached behaviour)?

感谢提前!

推荐答案

您将指定一个完整的 PropertyPath

var propertyInfo = typeof(IViewModel).GetProperty("Property");
var propertyPath = new PropertyPath("(0)", propertyInfo);
var binding = new Binding
{
    Path = propertyPath
};

有关传递给 PropertyPath 的语法的详细信息请参阅 PropertyPath.Path

For details on the syntax passed to PropertyPath above, see PropertyPath.Path.

这篇关于WPF - 绑定到明确实现的接口属性从代码背后/附加的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆