如何使 WPF 按钮看起来像一个链接?
我想在 WPF 中使用样式类似于链接的按钮.Microsoft 在其 Windows 对话框中执行此操作(似乎不一致).
I want to use buttons in WPF that are styled like links. Microsoft does this (seemingly inconsistently) in its Windows dialog boxes.
它们看起来像蓝色文本.并在鼠标光标悬停时更改颜色和下划线.
They look like blue text. And change color and underline when the mouse cursor hovers over.
我成功了.(感谢 Christian、Anderson Imes 和 MichaC) 但是,我不得不在我的按钮中放置一个 TextBlock
.
I got it working. (thanks to Christian, Anderson Imes, and MichaC) But, I had to put a TextBlock
inside my button.
如何改进我的样式——使其工作而不需要我的 Button 中的 TextBlock?
How can I improve my style—to make it work without requiring the TextBlock inside my Button?
<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>
你知道有一个 超链接 类/标签?它看起来像一个超链接,也可以用作按钮(可以使用 URI 和/或命令和/或点击事件).
Do you know there is a Hyperlink class/tag? It looks like a hyperlink and can work also as button (can use URI and/or command and/or click event).
用法示例:
<TextBlock>
<Hyperlink Command="{Binding SomeCommand, ElementName=window}" CommandParameter="{Binding}">Link
</Hyperlink>
</TextBlock>