2016-03-07 26 views
2

我的造型超链接如下:WPF控件模板继承风格从境内出现与风格“一个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创建一个新的样式,并明确引用这个按钮在页脚面板中。知道这是否可能,但不这样做会很有趣。

+0

您可以使用[BasedOn](https://msdn.microsoft.com/en-us/library/system.windows.style.basedon(v = vs.110).aspx),但这会让每个LinkBut​​ton都有footerpanel风格不只是在footerpanel内 –

回答

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样式中分配的颜色。

+0

这改变了'LinkBut​​ton'内的'超链接'始终是'FooterPanelLink' - 我希望有一种方法我可以说它应该使用全局样式,除非它在一个内部'FooterPanel',然后它应该使用'FooterPanelLink'。 – Will