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

Zac*_*son 34 windows wpf xaml styles wpf-controls

我想在WPF中使用像链接样式的按钮.Microsoft在其Windows对话框中执行此操作(看似不一致).

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

例:

Windows 7中的LinkBut​​ton

我搞定了.(感谢Christian,Anderson ImesMichaC)但是,我必须把一个TextBlock放在我的按钮内.

如何在不需要Button内的TextBlock的情况下改进我的风格?

用法XAML

<Button Style="{StaticResource HyperlinkLikeButton}">
    <TextBlock>Edit</TextBlock>
</Button>
Run Code Online (Sandbox Code Playgroud)

风格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>
Run Code Online (Sandbox Code Playgroud)

Mat*_*ský 36

你知道有超链接类/标签吗?它看起来像一个超链接,也可以作为按钮(可以使用URI和/或命令和/或点击事件).

编辑:

用法示例:

<TextBlock>                                
    <Hyperlink Command="{Binding SomeCommand, ElementName=window}" CommandParameter="{Binding}">Link
    </Hyperlink>
</TextBlock>
Run Code Online (Sandbox Code Playgroud)

  • 超级链接是一个框架内容元素,而不是一个框架元素。Silverlight有一个可以满足要求的“ HyperlinkBut​​ton”,但是可惜WPF没有。*叹* (2认同)
  • @Kent Boogaart:还有?只需将链接包装在TextBlock中即可完成.我很乐意在整个地方使用这样的超链接.它比扭曲按钮看起来像超链接更容易(也更有意义). (2认同)

Ayb*_*ybe 14

我参加派对有点晚了但是......

IMO最简单,最干净的方法:

<Style x:Key="HyperLinkButtonStyle" TargetType="Button">
    <Setter Property="Focusable" Value="False" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <TextBlock>
                    <Hyperlink>
                        <Run Text="{TemplateBinding Content}" />
                    </Hyperlink>
                </TextBlock>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)
  • 忠实,没有模仿:我们只使用Hyperlink本身
  • 尊重:保留焦点,这是一个重要方面

  • 为此,HyperLink的Command和CommandParameter必须绑定到按钮上的原始属性。 (2认同)

小智 7

使用以下样式或模板不需要使用TextBlock元素:

  <ControlTemplate x:Key="HyperlinkLikeButtonTemplate" TargetType="{x:Type Button}">
      <TextBlock x:Name="innerText" Foreground="{DynamicResource {x:Static SystemColors.HotTrackBrushKey}}" Cursor="Hand" >
        <ContentPresenter />
      </TextBlock>
    <ControlTemplate.Triggers>
      <Trigger Property="Button.IsMouseOver" Value="true">
        <Setter TargetName="innerText" Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />
        <Setter TargetName="innerText" Property="TextDecorations" Value="Underline" />
      </Trigger>
    </ControlTemplate.Triggers>
  </ControlTemplate>

  <Style x:Key="HyperlinkLikeButton" TargetType="{x:Type Button}">
    <Setter Property="Template" Value="{StaticResource HyperlinkLikeButtonTemplate}" />
  </Style> 
Run Code Online (Sandbox Code Playgroud)

用法XAML

<Button Style="{StaticResource HyperlinkLikeButton}" Content="Edit" />
Run Code Online (Sandbox Code Playgroud)

要么

<Button Style="{StaticResource HyperlinkLikeButton}">
    Edit
</Button>
Run Code Online (Sandbox Code Playgroud)

或者您可以直接使用模板

<Button Template="{StaticResource HyperlinkLikeButtonTemplate}" Content="Edit" />
Run Code Online (Sandbox Code Playgroud)