Welcome to MSDN Blogs Sign in | Join | Help

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

 

 

Published Tuesday, February 06, 2007 5:07 AM by irenak
Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# WPF: Crear un control Linkbutton

Tuesday, February 06, 2007 1:37 PM by Code Factory

Windows Presentation Foundation nos aporta la funcionalidad base para desde ella poder crear controles

# WPF: Crear un control Linkbutton

Tuesday, February 06, 2007 2:26 PM by CODE FACTORY - Proof of Concept

Windows Presentation Foundation nos aporta la funcionalidad base para desde ella poder crear controles

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

Wednesday, February 07, 2007 12:45 PM by John Lewicki

What about System.Windows.Documents.Hyperlink?  Is that not suitable for some reason?

# SYSK 281: Reply to John Lewicki

Wednesday, February 07, 2007 3:17 PM by irenak

Didn't work for me in a class derived from System.Windows.Window...  Were you able to use it successfully in <Window>, not <Page> or <FlowDocument>?

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

Saturday, July 28, 2007 3:15 AM by c lewelyn

Not the first time I have found a great nugget of info on your blog.

Thanks a lot!

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker