【问题标题】:How bind to TemplatedParent from DataTemplate defined in a style?如何从样式中定义的 DataTemplate 绑定到 TemplatedParent?
【发布时间】:2013-08-29 11:23:14
【问题描述】:

我正在开发一个从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>

我想将DataTemplateBorderBackground 属性绑定到MyItemsControl 的依赖属性。

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

没有任何效果。我在这里错过了什么?

【问题讨论】:

  • ControlTemplate 中,您可以使用 TemplateBinding。但这仅适用于ControlTemplates...我会尝试祖先的方式。
  • 我试过了 - 但无法让它工作。所以要么这不起作用 - 要么我犯了一个错误......

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


【解决方案1】:

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

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

只是为了在这里测试,我绑定到 ItemsControl 的 Name 属性,即“黄色” - 它可以工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-11
    • 2011-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-09
    • 2012-01-01
    相关资源
    最近更新 更多