6

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

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

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

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

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

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

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

提前谢谢了!

4

1 回答 1

10

您将指定一个完整的PropertyPath

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

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

于 2012-10-12T07:59:40.673 回答