views:

7178

answers:

5
+4  Q: 

Link button in wpf

Hey Guys,

How can I make Button to look like LinkButton, and I don't want to use Hyperlink...!!

Any suggestions

+2  A: 

Why do you not want to use Hyperlink?

<Button>
    <Hyperlink>
</Button>
Paul Betts
Coz, Whenever I try to get parent in Visual tree it throws an Exception saying Hyperlink is not visual or Visual3d
Prashant
I want to use this. I got it working but I can't seem to get rid of the shadow on the edges of the button. How do you do that?
Donny V.
+9  A: 

If you don't want any of the normal Button style and just want something that looks like a hyperlink you could start with this

    <Button Margin="5" Content="Test" Cursor="Hand">
        <Button.Template>
            <ControlTemplate TargetType="Button">
                <TextBlock TextDecorations="Underline">
                    <ContentPresenter />
                </TextBlock>
            </ControlTemplate>
        </Button.Template>
        <Button.Style>
            <Style TargetType="Button">
                <Setter Property="Foreground" Value="Blue" />
                <Style.Triggers>
                    <Trigger Property="IsMouseOver" Value="true">
                        <Setter Property="Foreground" Value="Red" />
                    </Trigger>
                </Style.Triggers>
            </Style>
        </Button.Style>
    </Button>
MichaC
+1  A: 

You can also do this:

<TextBlock>
    <Hyperlink>This is my link..</Hyperlink>
</TextBlock>
geosteve
Unfortunately, this doesn't behave as one might expect. If you're an XBAP (in-browser app), then it behaves analogously to the winform's version. If your host is a NavigationWindow or if the control is within a Frame, the hyperlink will only navigate to xaml pages within the application. If none of these are true it won't work.
Will
+2  A: 

Here's MichaC's suggestion implemented as a Style that you can reuse on any button:

<Style x:Key="LinkButton" TargetType="Button">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <TextBlock TextDecorations="Underline">
                    <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>
Anderson Imes
Made this a community wiki, considering 99% of this answer is stolen from MichaC :)
Anderson Imes
+1  A: 
<Style x:Key="LinkButton" TargetType="Button">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
              <ControlTemplate.Resources>
                <Style TargetType="{x:Type TextBlock}">
                  <Setter Property="TextDecorations" Value="Underline" />
                </Style>
              </ControlTemplate.Resources>
              <ContentPresenter />
            </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>

MichaC's and Anderson's version placed the underline slightly wrong, here is an updated version that will just add an underline to any TextBlock that are inside the content presenter.

Christian