2

I'm styling hyperlinks that appear within a border with the style "FooterPanel" as follows:

<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>

I also now have created a style to create a button as a hyperlink (so I can get properties such as IsDefault and IsCancel on a hyperlink):

<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>

Normal hyperlinks within FooterPanel receive the FooterPanelLinkBrush foreground, but if I use LinkButton within a FooterPanel, the style is not applied. Is there a way to get the ControlTemplate to inherit the styles within the FooterPanel instead of any global hyperlink styles?

Edit:

According to this answer https://stackoverflow.com/a/9166963/2383681 there is special handling that means the Hyperlink won't receive the styles defined in the FooterPanel as it's not derived from Control.

I'm not sure what I'm trying to do is therefore possible without some code-behind, so I think I'm just going to workaround this and create a new style for FooterPanelLinkButton and explicitly reference this for the buttons that are in a footer panel. It would be interesting to know if this is possible however without doing that.

Community
  • 1
  • 1
Will
  • 46
  • 5
  • You could use [BasedOn](https://msdn.microsoft.com/en-us/library/system.windows.style.basedon(v=vs.110).aspx) but that would make every LinkButton have the footerpanel style not just ones within the footerpanel – Gordon Allocman Mar 07 '16 at 18:30

1 Answers1

1

You can create a separate Style for the HyperLink:

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

And then use this Style in the Resources of the FooterPanel and LinkButton styles in the following way:

<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>

This way the HyperLink inside the LinkButton will use the color you assigned in the FooterPanelLink style.

Szabolcs Dézsi
  • 8,743
  • 21
  • 29
  • This changes the `Hyperlink` inside the `LinkButton` to always be the `FooterPanelLink` - I'm hoping there's a way I can say it should use the global styles, unless it's inside a `FooterPanel` and it should then use `FooterPanelLink`. – Will Mar 08 '16 at 10:04