views:

108

answers:

1

I would like to perform a typecast within a binding declaration within code (C#). Here's a quick chunk of code illustrating my situation:

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);

Here's the error I receive - not surprisingly, VS complains about not finding the path :

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  A: 

Sorry, but you can't do it that way, the path needs to be "direct". However, that's what converters should do; you should provide one which also performs any casting you need.

kek444
That's what I was afraid of. I was able to successfully use a converter but wanted to see if I could wire it up directly. Thanks!
Joel