绑定路径中的TypeCast

时间:2009-07-21 13:46:33

标签: c# wpf data-binding

我想在代码(C#)中的绑定声明中执行类型转换。这里有一小段代码说明了我的情况:

Binding aBinding = new Binding();
aBinding.Mode = BindingMode.OneTime;
aBinding.ElementName = "FullPagePageViewGrid";
//aBinding.Path = new PropertyPath("((IPageLayout)Children[0])"); // What I'd like to do - causes error
aBinding.Path = new PropertyPath("Children[0]");
aBinding.Converter = new IsSelectedTextBoldConverter();
this.aLabel.SetBinding(Label.ContentProperty, aBinding);

这是我收到的错误 - 毫不奇怪,VS抱怨找不到路径:

System.Windows.Data Error: 39 : BindingExpression path error: '((IPageLayout)Children[0])' property not found on 'object' ''Grid' (Name='FullPagePageViewGrid')'. BindingExpression:Path=((IPageLayout)Children[0]); DataItem='Grid' (Name='FullPagePageViewGrid'); target element is 'Label' (Name='aLabel'); target property is 'Content' (type 'Object')

1 个答案:

答案 0 :(得分:1)

很抱歉,但你不能这样做,路径需要“直接”。但是,这是转换器应该做什么;你应该提供一个也可以进行你需要的任何铸造。