我正在处理关于链接按钮的这个问题的答案:
https://stackoverflow.com/a/3564706/945
问题是TextDecoration Underline样式仅应用于自动生成的TextBlocks。
<Button Style="{StaticResource LinkButton}">Text</Button>
'文字'带下划线
<Button Style="{StaticResource LinkButton}"><TextBlock Text='Text' /></Button>
'文字'没有下划线
为什么它不适用于内容中的任何TextBlock?
这是风格的相关部分:
<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>
答案 0 :(得分:3)
我相信当您将一个Framework元素放在ContentControl中时,不会应用该模板。如果您还将TextBlock样式声明为Button的资源,则它在两种情况下都有效。
<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>
答案 1 :(得分:1)
答案 2 :(得分:0)
问题是您设置了[object Object]
。如果设置密钥,则隐式样式将不起作用。取下钥匙,它将应用于所有按钮