2009-07-21 101 views
0

我想在代码(C#)内的绑定声明中执行一个类型转换。这里的代码说明我的情况的快速块:TypeCast在绑定路径内

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

这就是我所害怕的。我能够成功地使用一个转换器,但想看看我能否直接连接它。谢谢! – Joel 2009-07-21 13:58:59