【发布时间】: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 中使用 LinkButton,则不会应用该样式。有没有办法让 ControlTemplate 继承 FooterPanel 中的样式而不是任何全局超链接样式?
编辑:
根据这个答案https://stackoverflow.com/a/9166963/2383681 有特殊处理,这意味着Hyperlink 不会接收FooterPanel 中定义的样式,因为它不是从Control 派生的。
我不确定我正在尝试做的事情是否可能没有一些代码隐藏,所以我想我只是要解决这个问题并为FooterPanelLinkButton 创建一个新样式并明确引用这个页脚面板中的按钮。如果不这样做,知道这是否可能会很有趣。
【问题讨论】:
-
您可以使用BasedOn,但这会使每个链接按钮都具有页脚面板样式,而不仅仅是页脚面板中的样式