Link button in wpf

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

Any suggestions

+78
wpf wpf-controls
Apr 23 '09 at 5:41
source share
6 answers

If you don’t want any regular Button style and want something that looks like a hyperlink, you can 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> 

Here, as a style:

 <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" /> <Style.Triggers> <Trigger Property="IsMouseOver" Value="true"> <Setter Property="Foreground" Value="Red" /> </Trigger> </Style.Triggers> </Style> 

and you can use it as follows:

 <Button Style="{StaticResource LinkButton}" Content="Clicky" /> 
+130
Apr 23 '09 at 8:13
source share

Here the MichaC proposal is implemented as Style , which can be used 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> 
+29
May 27 '10 at 3:45 a.m.
source share
 <Style x:Key="LinkButton" TargetType="Button" BasedOn="{StaticResource ResourceKey={x:Type Button}}" > <Setter Property="Width" Value="Auto"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="Button"> <ContentPresenter Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}" VerticalAlignment="Center" > <ContentPresenter.Resources> <Style TargetType="{x:Type TextBlock}"> <Setter Property="TextDecorations" Value="Underline" /> </Style> </ContentPresenter.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> 

The version of MichaC and Anderson placed the underscore a bit wrong, here is an updated version that will simply add the underscore to any TextBlock that are inside the ContentPresenter .

+29
Aug 25 '10 at 10:02
source share

The easiest way (I do this in my application):

 <TextBlock Name="..." Text="..." Cursor="Hand" Foreground="Blue" TextDecorations="Underline" MouseLeftButtonUp=..." /> 

you have full control over TextDecoration, for example. Change pen style or offset. look at this link to find out more: http://msdn.microsoft.com/en-us/library/system.windows.textdecorations.underline.aspx

+9
Jan 05 2018-11-11T00: 00Z
source share

Another solution using Hyperlink is inside TextBlock .

 <TextBlock> <Hyperlink Click="..."> <TextBlock Text="Link text" /> </Hyperlink> </TextBlock> 
+6
Jun 02 '15 at 15:20
source share

Why don't you want to use a hyperlink?

 <Button> <Hyperlink> </Button> 
+1
Apr 23 '09 at 5:43
source share



All Articles