我有这个代码
<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 的属性或 HyperlinkButton 中的任何其他元素的样式,就像在CSS中一样
HyperlinkButton.*
{
Background : red;
}
TIA
答案 0 :(得分:0)
您需要使用绑定
来解析背景属性将此添加到TextBlock:
{Binding Path=Background, RelativeSource={RelativeSource AncestorType={x:Type HyperlinkButton}}}
答案 1 :(得分:0)
TextBlock
没有要设置的Background
属性。您需要将其包装在另一个控件Border
中,例如:
<HyperlinkButton.Content>
<Border Background="Red">
<TextBlock Text="Sample Text"></TextBlock>
</Border>
</HyperlinkButton.Content>
当然,您也可以使用样式设置边框背景:
<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>
编辑:
您不能影响XAML中外部元素的样式中的嵌套元素。您可以在样式中设置ContentTemplate
:
<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>