【问题标题】:WPF ControlTemplate inherit style from parent resourcesWPF ControlTemplate 从父资源继承样式
【发布时间】:2016-03-07 17:57:05
【问题描述】:

我正在使用样式“FooterPanel”对出现在边框内的超链接进行样式设置,如下所示:

<Style x:Key="FooterPanel" TargetType="{x:Type Border}">
    <Style.Resources>
        <Style TargetType="{x:Type Hyperlink}">
            <Setter Property="Foreground" Value="{StaticResource FooterPanelLinkBrush}"/>
        </Style>
    </Style.Resources>
</Style>

我现在还创建了一种样式来将按钮创建为超链接(因此我可以在超链接上获取 IsDefault 和 IsCancel 等属性):

<Style x:Key="LinkButton" TargetType="{x:Type Button}">
    <Setter Property="Focusable" Value="False"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center">
                    <Hyperlink Command="{TemplateBinding Command}" CommandParameter="{TemplateBinding CommandParameter}">
                        <Run Text="{TemplateBinding Content}"/>
                    </Hyperlink>
                </TextBlock>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

FooterPanel 中的普通超链接接收 FooterPanelLinkBrush 前景,但如果我在 FooterPanel 中使用 LinkBut​​ton,则不会应用该样式。有没有办法让 ControlTemplate 继承 FooterPanel 中的样式而不是任何全局超链接样式?

编辑:

根据这个答案https://stackoverflow.com/a/9166963/2383681 有特殊处理,这意味着Hyperlink 不会接收FooterPanel 中定义的样式,因为它不是从Control 派生的。

我不确定我正在尝试做的事情是否可能没有一些代码隐藏,所以我想我只是要解决这个问题并为FooterPanelLinkButton 创建一个新样式并明确引用这个页脚面板中的按钮。如果不这样做,知道这是否可能会很有趣。

【问题讨论】:

  • 您可以使用BasedOn,但这会使每个链接按钮都具有页脚面板样式,而不仅仅是页脚面板中的样式

标签: c# wpf xaml


【解决方案1】:

您可以为HyperLink 创建一个单独的Style

<Style x:Key="FooterPanelLink" TargetType="{x:Type Hyperlink}">
    <Setter Property="Foreground" Value="{StaticResource FooterPanelLinkBrush}"/>
</Style>

然后在FooterPanelLinkButton样式的Resources中使用这个Style,方法如下:

<Style x:Key="FooterPanel" TargetType="{x:Type Border}">
    <Style.Resources>
        <Style TargetType="Hyperlink" BasedOn="{StaticResource FooterPanelLink}" />
    </Style.Resources>
</Style>

<Style x:Key="LinkButton" TargetType="{x:Type Button}">
    <Style.Resources>
        <Style TargetType="Hyperlink" BasedOn="{StaticResource FooterPanelLink}" />
    </Style.Resources>

    <Setter Property="Focusable" Value="False"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center">
                    <Hyperlink Command="{TemplateBinding Command}" CommandParameter="{TemplateBinding CommandParameter}">
                        <Run Text="{TemplateBinding Content}"/>
                    </Hyperlink>
                </TextBlock>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

这样LinkButton 中的HyperLink 将使用您在FooterPanelLink 样式中指定的颜色。

【讨论】:

  • 这会将LinkButton 中的Hyperlink 更改为始终为FooterPanelLink - 我希望有一种方法可以说它应该使用全局样式,除非它在@987654335 内@ 然后它应该使用FooterPanelLink
猜你喜欢
  • 1970-01-01
  • 2010-12-11
  • 1970-01-01
  • 2014-07-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-07
  • 2011-04-06
相关资源
最近更新 更多