WPF绑定:访问绑定对象的子属性

时间:2013-05-14 16:39:27

标签: wpf binding

我有一个名为Item的{​​{1}}属性的控件, X有一个名为Item的{​​{1}}类型的属性,Tag有一些其他属性(YTag,...)。

我为我的控件编写了一个控件模板,我需要访问caption的属性(locationTag,...等等)所以我已经使用与caption location绑定,并RelativeSource我尝试使用TemplatedParent,如下所示:

Path

但是我收到以下错误: 找不到“Path=Item.Tag.caption”的 <ControlTemplate TargetType="y:myControl"> <Rectangle> <Rectangle.Fill> <SolidColorBrush Color="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Item.Tag.caption}" /> </Rectangle.Fill> </Rectangle> </ControlTemplate> 属性,因为Tag并且我没有收到任何结果。

1 个答案:

答案 0 :(得分:2)

解决方案是在访问属性之前在Path变量中执行强制转换,如下所示:Path=Item.(myNameSpace:X.Tag).(myNameSpace:Y.caption) 有关详细信息,请查看WPF Binding : Casting in binding path