【发布时间】:2011-07-04 14:05:11
【问题描述】:
我想在 WPF 中使用样式类似于链接的按钮。 Microsoft 在其 Windows 对话框中这样做(似乎不一致)。
它们看起来像蓝色文本。并在鼠标悬停时更改颜色和下划线。
示例:
我让它工作了。 (感谢Christian、Anderson Imes 和MichaC)但是,我必须在按钮中添加TextBlock。
如何改进我的风格——让它在我的 Button 中不需要 TextBlock 的情况下工作?
使用 XAML
<Button Style="{StaticResource HyperlinkLikeButton}">
<TextBlock>Edit</TextBlock>
</Button>
样式 XAML
<Style x:Key="HyperlinkLikeButton" TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<ContentPresenter />
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HotTrackBrushKey}}" />
<Setter Property="Cursor" Value="Hand" />
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<ControlTemplate.Resources>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="TextDecorations" Value="Underline" />
</Style>
</ControlTemplate.Resources>
<ContentPresenter />
</ControlTemplate>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
【问题讨论】:
-
你的按钮在被点击时实际上在做什么?只是导航?
标签: wpf windows xaml wpf-controls styles