2

我有一个带有名为 type 的属性的控件,有一个名为ItemtypeX的 属性Item,还有一些其他属性(, ,...)。TagYTagcaptionlocation

我已经为我的控件编写了一个控件模板,并且我需要访问Tag' 的属性(captionlocation、...等),所以我使用了 Binding with 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找不到' object'的属性,因为data item is null我没有得到任何结果。

4

1 回答 1

2

解决方案是Path在访问属性之前在变量中执行强制转换,如下所示:Path=Item.(myNameSpace:X.Tag).(myNameSpace:Y.caption) 有关更多信息,请查看WPF 绑定:绑定路径中的强制转换

于 2013-05-17T10:44:49.227 回答