i have this code
我有这个代码
<HyperlinkButton Style="{StaticResource HyperLink-Navi-Container}">
<HyperlinkButton.Content>
<TextBlock Text="Sample Text"></TextBlock>
</HyperlinkButton.Content>
</HyperlinkButton>
and this style
而这种风格
<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>
now my question is how to style the properties of the TextBlock or any other elements inside the HyperlinkButton just like in CSS
现在我的问题是如何设置TextBlock的属性或HyperlinkButton中的任何其他元素的样式,就像在CSS中一样
HyperlinkButton.*
{
Background : red;
}
TIA
2 个解决方案
#1
0
You need to resolve background property using binding
您需要使用绑定来解析背景属性
Add this to TextBlock:
将其添加到TextBlock:
{Binding Path=Background, RelativeSource={RelativeSource AncestorType={x:Type HyperlinkButton}}}
#2
0
TextBlock
doesn't have a Background
property to set. You'll need to wrap it in another control, Border
for example:
TextBlock没有要设置的Background属性。你需要将它包装在另一个控件中,例如Border:
<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:
您不能影响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>
#1
0
You need to resolve background property using binding
您需要使用绑定来解析背景属性
Add this to TextBlock:
将其添加到TextBlock:
{Binding Path=Background, RelativeSource={RelativeSource AncestorType={x:Type HyperlinkButton}}}
#2
0
TextBlock
doesn't have a Background
property to set. You'll need to wrap it in another control, Border
for example:
TextBlock没有要设置的Background属性。你需要将它包装在另一个控件中,例如Border:
<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:
您不能影响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>