How do I make a WPF button look like a link? How do I make a WPF button look like a link? windows windows

How do I make a WPF button look like a link?


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:

<TextBlock>                                    <Hyperlink Command="{Binding SomeCommand, ElementName=window}" CommandParameter="{Binding}">Link    </Hyperlink></TextBlock>


I'm a little late for the party but ...

The simplest and cleanest approach 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>
  • Faithful, no emulation : we just use the Hyperlink itself
  • Respectful : the focus, which is an important aspect, is preserved


Using the following style or template does not require you to use the TextBlock element:

  <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> 

Usage XAML

<Button Style="{StaticResource HyperlinkLikeButton}" Content="Edit" />

or

<Button Style="{StaticResource HyperlinkLikeButton}">    Edit</Button>

or you can use the template directly

<Button Template="{StaticResource HyperlinkLikeButtonTemplate}" Content="Edit" />