ItemsPanelTemplate中的TemplateBinding

时间:2009-03-29 09:16:33

标签: silverlight controls

我正在Silverlight中构建一个自定义ItemsControl,其中包括(在其他方面)允许项目在运行时水平或垂直显示。如何将ItemsPanel的Orientation属性绑定到我的父控件的Orientation属性?我已经尝试过使用TemplateBinding(它在ControlTemplate中工作)但似乎没有在ItemsPanelTemplate中工作,我做错了什么?

<Style TargetType="CustomItemsControl">
    <Setter Property="ItemsPanel">
        <Setter.Value>
            <ItemsPanelTemplate>
                <StackPanel Orientation="{TemplateBinding Orientation}" />
            </ItemsPanelTemplate>
        </Setter.Value>
    </Setter>
</Style>

1 个答案:

答案 0 :(得分:7)

使用RelativeSource:

<Style TargetType="CustomItemsControl">
    <Setter Property="ItemsPanel">
        <Setter.Value>
            <ItemsPanelTemplate>
                <StackPanel Orientation="{Binding Orientation, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type CustomItemsControl}}}" />
            </ItemsPanelTemplate>
        </Setter.Value>
    </Setter>
</Style>

评论后编辑:Silverlight不支持RelativeSource,但Colin Eberhardt的this post解释了如何手动实现。