ContentPresenter中的自定义控件前台

时间:2016-04-27 18:18:53

标签: wpf xaml custom-controls controltemplate attached-properties

我有一个自定义控件,它实际上只适用于特定类型的模板选择:

<Style TargetType="MyControl">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="MyControl">
                <Path Data="..." Fill="{TemplateBinding Foreground}"/>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

然后我有一个自动应用的模板:

<ContentPresenter Control.Foreground="Red" Content="This Works"/>

工作正常,我可以放入XAML并且显示正确。

我的问题是试图让Foreground在内容演示者中正确显示。请考虑以下内容演示者:

{{1}}

显示的文字为红色。但是,如果我将内容设为&#34; MyControl&#34;,它就不会 - 它会在可视化树上查找要继承的前景。它似乎忽略了Control.Foreground。如何让我的自定义控件在内容演示者上关注Control.Foreground?

如果重要,这个内容展示者在TabItem的控件模板中,尝试获取其中一个&#34; MyControl&#34;要用作TabItem标题的实例。

0 个答案:

没有答案
相关问题