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


 


 

Comments (5)

  1. Code Factory says:

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

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

  3. John Lewicki says:

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

  4. irenake says:

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

  5. c lewelyn says:

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

    Thanks a lot!

Skip to main content