SYSK 281: Windows Presentation Foundation (WPF): The Missing LinkButton

To my surprise, System.Windows.Controls does not have LinkButton! Perhaps Windows forms shouldn’t use links? But since my UI design team insisted on having them, I’ve created my own LinkButton (code below)… As always – use at your own risk… this code has not been rigorously tested.

 

1.  Declare the following style somewhere in the resources section (e.g. Window.Resources, Application.Resources, etc.)

 

<Style x:Key="LinkButtonStyle" TargetType="{x:Type Button}">

      <Setter Property="SnapsToDevicePixels" Value="true" />

      <Setter Property="OverridesDefaultStyle" Value="true" />

      <Setter Property="Template">

            <Setter.Value>

                  <ControlTemplate TargetType="{x:Type Button}">

                        <TextBlock x:Name="tb" Background="{x:Null}" Cursor="Hand" TextDecorations="Underline" TextWrapping="Wrap" >

                              <ContentPresenter VerticalAlignment="Center" RecognizesAccessKey="true"/>

                        </TextBlock>

                        <ControlTemplate.Triggers>

                              <Trigger Property="IsPressed" Value="true" >

                                    <Setter Property="Cursor" Value="Hand" />

                                    <Setter TargetName="tb" Property="BitmapEffect">

                                          <Setter.Value>

                                                <DropShadowBitmapEffect ShadowDepth="1" Direction="330" Color="Black" Opacity="0.5" Softness="0.25" />

                  </Setter.Value>

                                    </Setter>

                              </Trigger>

                        </ControlTemplate.Triggers>

                  </ControlTemplate>

            </Setter.Value>

      </Setter>

</Style>

 

2.  Use it by adding Style="{StaticResource LinkButtonStyle}" attribute to your buttons:

 

<Button Click="YourClickEventHandler" Style="{StaticResource LinkButtonStyle}"

      Foreground="Blue" Margin="10,10,10,10"

HorizontalAlignment="Left" Width="120px" >

Your link text here...

</Button>

 

3.  Remember to replace YourClickEventHandler with your own delegate