我想在 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>
最佳答案
你知道有一个Hyperlink吗?类/标签?它看起来像一个超链接,也可以用作按钮(可以使用 URI 和/或命令和/或单击事件)。
编辑:
使用示例:
<TextBlock>
<Hyperlink Command="{Binding SomeCommand, ElementName=window}" CommandParameter="{Binding}">Link
</Hyperlink>
</TextBlock>
关于wpf - 如何使 WPF 按钮看起来像一个链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5122334/