如何正确地将样式应用到内容展示器 [英] How Do I Correctly Apply a Style to Content Presenter
本文介绍了如何正确地将样式应用到内容展示器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在回答这个关于链接按钮的问题:
I'm working with this answer to this question about link buttons:
https://stackoverflow.com/a/3564706/945
问题在于 TextDecoration Underline 样式仅应用于自动生成的 TextBlocks.
The problem is that the TextDecoration Underline style is only being applied to autogenerated TextBlocks.
<Button Style="{StaticResource LinkButton}">Text</Button>
'文本'带有下划线
<Button Style="{StaticResource LinkButton}"><TextBlock Text='Text' /></Button>
'文本'没有下划线
为什么它不适用于内容中的任何 TextBlock?
Why is it not applying to any TextBlock within the content?
这是样式的相关部分:
<Style x:Key="LinkButton"
TargetType="Button"
BasedOn="{StaticResource ResourceKey={x:Type Button}}"
>
<Setter Property="Width" Value="Auto"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<ContentPresenter Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}"
VerticalAlignment="Center"
>
<ContentPresenter.Resources>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="TextDecorations" Value="Underline" />
</Style>
</ContentPresenter.Resources>
</ContentPresenter>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
推荐答案
我相信当您将 Framework 元素放入 ContentControl 时,不会应用模板.如果您还将 TextBlock 样式声明为 Button 的资源,则这两种情况都适用.
I belive that when you put a Framework element inside a ContentControl, the Template is not applied. If you also declare your TextBlock style as a Button's resource, it works in both cases.
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:local="clr-namespace:WpfApplication1"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="MainWindow"
Width="525"
Height="350"
mc:Ignorable="d">
<Window.Resources>
<Style x:Key="LinkButton"
BasedOn="{StaticResource ResourceKey={x:Type Button}}"
TargetType="Button">
<Setter Property="Width" Value="Auto" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<ContentPresenter VerticalAlignment="Center"
Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}" >
<ContentPresenter.Resources>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="TextDecorations" Value="Underline" />
</Style>
</ContentPresenter.Resources>
</ContentPresenter>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Window.DataContext>
<local:ViewModel />
</Window.DataContext>
<Grid x:Name="LayoutRoot">
<StackPanel>
<Button Style="{StaticResource LinkButton}">Text</Button>
<Button Style="{StaticResource LinkButton}">
<Button.Resources>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="TextDecorations" Value="Underline" />
</Style>
</Button.Resources>
<TextBlock Text="Text" />
</Button>
</StackPanel>
</Grid>
</Window>
这篇关于如何正确地将样式应用到内容展示器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文