2009-07-21 100 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