1

我有这个代码

<HyperlinkButton Style="{StaticResource HyperLink-Navi-Container}">
      <HyperlinkButton.Content>
             <TextBlock Text="Sample Text"></TextBlock>
      </HyperlinkButton.Content>
</HyperlinkButton>

和这种风格

<Style x:Key="HyperLink-Navi-Container" TargetType="HyperlinkButton">
    <Setter Property="Height" Value="50"></Setter>
    <Setter Property="Background">
        <Setter.Value>
            <LinearGradientBrush StartPoint="1, 0.5" EndPoint="1, 2">
                <GradientStop Color="White" Offset="0.75"></GradientStop>
                <GradientStop Color="Gray"></GradientStop>
            </LinearGradientBrush>
        </Setter.Value>
    </Setter>
    <Setter Property="Margin" Value="2,2,2,0"></Setter>
    <Setter Property="Foreground" Value="Orange"></Setter>

</Style>

现在我的问题是如何设置TextBlock的属性或HyperlinkBut ​​ton内的任何其他元素的样式,就像在 CSS 中一样

HyperlinkButton.*
{
  Background : red;
}

TIA

4

2 回答 2

0

TextBlock doesn't have a Background property to set. You'll need to wrap it in another control, Border for example:

<HyperlinkButton.Content>
    <Border Background="Red">
        <TextBlock Text="Sample Text"></TextBlock>
    </Border>
</HyperlinkButton.Content>

Of course you can also use a style to set border background:

<Style x:Key="HyperlinkBorder" TargetType="Border">
    <Setter Property="Background" Value="Red" />
</Style>

<HyperlinkButton.Content>
    <Border Style="{StaticResource HyperlinkBorder}">
        <TextBlock Text="Sample Text"></TextBlock>
    </Border>
</HyperlinkButton.Content>

EDIT:

You can't affect nested elements from a style od an outer element in XAML. You could set the ContentTemplate in the style though:

<DataTemplate x:Key="HyperlinkTemplate">
    <Border Background="Red">
        <TextBlock Text="Sample Text"></TextBlock>
    </Border>
</DataTemplate>

<Style x:Key="HyperLink-Navi-Container" TargetType="HyperlinkButton">
    <Setter Property="Height" Value="50"></Setter>
    <Setter Property="Background">
        <Setter.Value>
            <LinearGradientBrush StartPoint="1, 0.5" EndPoint="1, 2">
                <GradientStop Color="White" Offset="0.75"></GradientStop>
                <GradientStop Color="Gray"></GradientStop>
            </LinearGradientBrush>
        </Setter.Value>
    </Setter>
    <Setter Property="Margin" Value="2,2,2,0"></Setter>
    <Setter Property="Foreground" Value="Orange"></Setter>
    <Setter Property="ContentTemplate" Value="{StaticResource HyperlinkTemplate}" />
</Style>
于 2013-05-22T04:52:53.633 回答
0

您需要使用绑定来解析背景属性

将此添加到 TextBlock:

{Binding Path=Background, RelativeSource={RelativeSource AncestorType={x:Type HyperlinkButton}}}
于 2013-05-22T04:08:05.037 回答