ItemsPanelTemplate中的TemplateBinding

Pau*_*vis 2 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>
Run Code Online (Sandbox Code Playgroud)

Ken*_*art 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>
Run Code Online (Sandbox Code Playgroud)

评论后编辑:Silverlight不支持RelativeSource,但Colin Eberhardt的这篇文章解释了如何手动实现它.