wpf - 如何使 WPF 按钮看起来像一个链接?

标签 wpf windows xaml wpf-controls styles

我想在 WPF 中使用样式类似于链接的按钮。 Microsoft 在其 Windows 对话框中这样做(看似不一致)。

它们看起来像蓝色文字。并在鼠标光标悬停时更改颜色和下划线。

示例:

LinkButton in Windows 7

我成功了。 (感谢 ChristianAnderson ImesMichaC )但是,我不得不在我的按钮中放置一个 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/

相关文章:

wpf - 在 WPF TabControl 上 - 我可以在选项卡标题旁边添加内容吗?

windows - 我如何制作一个批处理程序来输入

windows - C :\WINDOWS\ASSEMBLY\directory?中的那些东西是什么

c# - 如何在 xaml wpf 的 ListView 中使用绑定(bind)到 ToggleButton 的 Popup?

c# - 设计期间的 MarkupExtension.ProvideValue(IServiceProvider serviceProvider)

c# - wpf 以编程方式设置排序,以便将标题切换为已排序

windows-phone-7 - 无法使用 xmlns :cl ="FooNamespace" in XAML

c# - 在 xaml 中定义时如何避免创建多个弹出窗口?

c# - 在 WPF 中,如何在不使用任何事件的情况下判断鼠标左键当前是否按下?

windows - 有没有办法在 Windows PowerShell 中获得 Git 体验?