简体   繁体   中英

WPF ControlTemplate inherit style from parent resources

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM