繁体   English   中英

WPF - 从代码隐藏/附加行为绑定到显式实现的接口属性

[英]WPF - binding to an explicitly implemented interface property from code behind/attached behaviour

我试图从代码隐藏设置绑定到显式实现的接口属性。 代码隐藏绑定的原因是绑定属性的路径只能在运行时确定。

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

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

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

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

因为WPF能够获取命名空间映射。

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

提前谢谢了!

您将指定一个完整的PropertyPath

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM