92

如何让 Button 看起来像 LinkBut​​ton,而且我不想使用超链接...!!

有什么建议么

4

7 回答 7

154

如果您不想要任何普通的 Button 样式,而只想要看起来像超链接的东西,您可以从这个开始

<Button Margin="5" Content="Test" Cursor="Hand">
    <Button.Template>
        <ControlTemplate TargetType="Button">
            <TextBlock TextDecorations="Underline">
                <ContentPresenter />
            </TextBlock>
        </ControlTemplate>
    </Button.Template>
    <Button.Style>
        <Style TargetType="Button">
            <Setter Property="Foreground" Value="Blue" />
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="true">
                    <Setter Property="Foreground" Value="Red" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </Button.Style>
</Button>

这与样式相同:

<Style
    x:Key="LinkButton"
    TargetType="Button">
    <Setter
        Property="Template">
        <Setter.Value>
            <ControlTemplate
                TargetType="Button">
                <TextBlock
                    TextDecorations="Underline">
                <ContentPresenter /></TextBlock>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Setter
        Property="Foreground"
        Value="Blue" />
    <Style.Triggers>
        <Trigger
            Property="IsMouseOver"
            Value="true">
            <Setter
                Property="Foreground"
                Value="Red" />
        </Trigger>
    </Style.Triggers>
</Style>

你可以像这样使用它:

<Button Style="{StaticResource LinkButton}" Content="Clicky" />
于 2009-04-23T08:13:20.007 回答
35
<Style x:Key="LinkButton" 
       TargetType="Button"
       BasedOn="{StaticResource ResourceKey={x:Type Button}}"
       >

    <Setter Property="Width" Value="Auto"/>

    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <ContentPresenter Content="{TemplateBinding Content}" 
                                  ContentTemplate="{TemplateBinding  ContentTemplate}"
                                  VerticalAlignment="Center"
                                  >
                    <ContentPresenter.Resources>
                        <Style TargetType="{x:Type TextBlock}">
                            <Setter Property="TextDecorations" Value="Underline" />
                        </Style>
                    </ContentPresenter.Resources>
                </ContentPresenter>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="Foreground" Value="Blue" />
    <Setter Property="Cursor" Value="Hand" />
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="true">
            <Setter Property="Foreground" Value="Red" />
        </Trigger>
    </Style.Triggers>
</Style>

MichaC 和 Anderson 的版本将下划线放置稍有错误,这是一个更新版本,它只会TextBlockContentPresenter.

于 2010-08-25T10:02:07.370 回答
30

这是 MichaC 的建议Style,您可以在任何按钮上重复使用:

<Style x:Key="LinkButton" TargetType="Button">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <TextBlock TextDecorations="Underline">
                    <ContentPresenter />
                </TextBlock>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="Foreground" Value="Blue" />
    <Setter Property="Cursor" Value="Hand" />
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="true">
            <Setter Property="Foreground" Value="Red" />
        </Trigger>
    </Style.Triggers>
</Style>
于 2010-05-27T03:45:38.523 回答
14

最简单的方法(我在我的应用程序中这样做):

<TextBlock Name="..."
   Text="..."
   Cursor="Hand"
   Foreground="Blue"
   TextDecorations="Underline"
   MouseLeftButtonUp=..."
/>

您可以完全控制 TextDecoration,例如更改笔样式或偏移量。查看此链接以了解更多信息:http: //msdn.microsoft.com/en-us/library/system.windows.textdecorations.underline.aspx

于 2011-01-05T09:06:28.860 回答
9

另一种使用的解决方案Hyperlink是放在里面TextBlock

<TextBlock>
    <Hyperlink Click="...">
        <TextBlock Text="Link text" />
    </Hyperlink>
</TextBlock>
于 2015-06-02T15:20:35.950 回答
2

为什么不想使用超链接?

<Button>
    <Hyperlink>
</Button>
于 2009-04-23T05:43:36.527 回答
0

所有提议的解决方案的组合:
完整的样式,与接受的版本一样,但没有硬编码值。

<Style
    x:Key="HyperlinkButton"
    TargetType="{x:Type Button}"
    BasedOn="{StaticResource {x:Type Button}}"
    >
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <TextBlock>
                    <Hyperlink
                        Command="{TemplateBinding Command}"
                        CommandTarget="{TemplateBinding CommandTarget}"
                        CommandParameter="{TemplateBinding CommandParameter}"
                        >
                        <ContentPresenter />
                    </Hyperlink>
                </TextBlock>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
于 2022-01-21T20:39:58.477 回答