如何使 WPF 按钮看起来像链接?
我想在 WPF 中使用样式类似于链接的按钮。 Microsoft 在其 Windows 对话框中这样做(看似不一致)。
它们看起来像蓝色文字。当鼠标光标悬停在上方时更改颜色和下划线。
示例:
我成功了。 (感谢克里斯蒂安,安德森·艾姆斯,以及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>
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.
Example:
I got it working. (thanks to Christian, Anderson Imes, and MichaC) But, I had to put a TextBlock
inside my button.
How can I improve my style—to make it work without requiring the TextBlock inside my Button?
Usage XAML
<Button Style="{StaticResource HyperlinkLikeButton}">
<TextBlock>Edit</TextBlock>
</Button>
Style 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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您知道有一个 超链接 类/标签?它看起来像一个超链接,也可以用作按钮(可以使用 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).
EDIT:
Example of usage:
我参加聚会有点晚了,但是...
最简单、最干净的方法 IMO :
I'm a little late for the party but ...
The simplest and cleanest approach IMO :
使用以下样式或模板不需要您使用 TextBlock 元素:
Usage XAML
或者
您可以直接使用模板
Using the following style or template does not require you to use the TextBlock element:
Usage XAML
or
or you can use the template directly
我认为问题在于按钮是内容控件,但链接样式仅适用于文本作为内容。这就是您的代码如此设计的原因。我认为我们可以通过指定文本块而不是内容呈现器来在控件模板中工作,但是什么属性绑定到它?所以我的建议是:子类按钮并声明字符串类型的依赖属性,并使用该属性绑定 ControlTemplate 中的文本。
I think the trouble is that button is a content control, but the link style works only with text as a content. This is the reason the code you have is designed that way. I think we can work in the control template by specifying a textblock instead of content presenter, but what property bind to it ? So my proposal is: subclass the button and declare a dependency property of type string and use that property to bind the text in the ControlTemplate.
除非我遗漏了一些东西,否则如果您是内联应用样式而不是全局应用样式,您就不能摆脱
TextBlock
的样式吗?例如:所以:
要启用单击,只需使用
MouseUp
事件。Unless I'm missing something, couldn't you get away with styling the
TextBlock
, if you're applying the styles inline as opposed to globally, anyway? For instance:And so:
To enable clicking, simply use the
MouseUp
event.