WPF LinkButton – Easy as pie

Where in the world is the LinkButton? I am sure its there somewhere but I just didn’t find it. So, what’s a guy to do? Get some style, that’s what.

WPF makes restyling controls incredibly easy. So much, I switched gears from a WinForms project to use WPF to make the interface look great without resorting to a million third party controls.

I assume you are familiar with WPF and know how to apply a style to a control. Well, apply this to a Button. Just paste the code below in your UserControl.Resources or Window.Resources section and then apply the Style={StaticResource LinkButton} to the button.

Note: I hate underlined links. You can add the underline by changing the TextFormatter on the TextBlock wrapping the content presenter.

 

  <Style x:Key="LinkButton" TargetType="Button">

            <Setter Property="Template">

                <Setter.Value>

                    <ControlTemplate TargetType="Button">

                        <TextBlock>

                    <ContentPresenter />

                        </TextBlock>

                    </ControlTemplate>

                </Setter.Value>

            </Setter>

            <Setter Property="Foreground" Value="Blue" />

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

            <Style.Triggers>

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

                    <Setter Property="Foreground" Value="Red" />

                </Trigger>

            </Style.Triggers>

        </Style>

Happy Coding!

Leave a Reply

Your email address will not be published. Required fields are marked *