如何从样式中定义的DataTemplate绑定到TemplatedParent?

时间:2013-08-29 11:23:15

标签: silverlight xaml styles custom-controls silverlight-5.0

我正在开发一个源自ItemsControl的自定义控件。在generic.xaml文件中,我为该控件创建了样式,并定义了ItemTemplate

<Style TargetType="local:MyItemsControl">
    <Setter Property="ItemTemplate">
        <Setter.Value>
            <DataTemplate>
                <Border Background="Red">
                    <!-- Other things in here -->
                </Border>
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>

我想将BackgroundBorder的{​​{1}}属性绑定到DataTemplate的依赖项属性。

如果在这里发现了几个问题,建议在绑定中使用MyItemsControl的元素名称,但这仅在定义使用控件的MyItemsControl时才有效。我还尝试绑定到ItemTemplate,将RelativeSource定义为祖先类型。

没有任何效果。我在这里缺少什么?

1 个答案:

答案 0 :(得分:3)

DependencyProperty的类型是什么?是Brush还是string? 这个简单的代码对我有用:

Background="{Binding Name, RelativeSource={RelativeSource AncestorType=ItemsControl}}" 

只是为了测试,我绑定到ItemsControl的Name属性为'Yellow' - 它可以工作。

相关问题