【问题标题】:TemplateBinding in ItemsPanelTemplateItemsPanelTemplate 中的模板绑定
【发布时间】:2010-10-16 05:08:05
【问题描述】:

我正在 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>

【问题讨论】:

    标签: silverlight controls


    【解决方案1】:

    使用相对来源:

    <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 解释了如何手动实现它。

    【讨论】:

    • 您好 Kent,感谢您的回答,不幸的是,我正在使用 Silverlight,它似乎不支持使用 RelativeSource。抱歉,我已经更新了我原来的问题,以澄清我正在使用 Silverlight。
    • 嗯,我明白了。在这种情况下,您可能需要查看这篇文章:scottlogic.co.uk/blog/wpf/2009/02/…
    • +1:我正在使用 WPF 并且遇到了同样的问题;你的回答真的很有帮助。
    猜你喜欢
    • 2012-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-06
    • 1970-01-01
    • 2014-10-20
    • 1970-01-01
    相关资源
    最近更新 更多