With all of the blog posts about Button styling in WPF since its release, it amazes me that there is very little out there about totally removing the Button default styling. I needed an image to be my button, but without overriding the control template for the button, you can’t remove the borders or get rid of the bluish mouse over effects. Below is a style for a borderless button, whose mouse over and IsPressed effect is a small size transformation to the content.
Borderless Button Style
- <Style x:Key="BorderlessButton" TargetType="{x:Type Button}">
- <Setter Property="Padding" Value="1"/>
- <Setter Property="Background" Value="Transparent" />
- <Setter Property="Template">
- <Setter.Value>
- <ControlTemplate TargetType="{x:Type Button}">
- <Border Name="border" Background="{TemplateBinding Background}">
- <ContentPresenter Name="content" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
- Margin="{TemplateBinding Padding}"
- RecognizesAccessKey="True"
- SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
- VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
- </Border>
- <ControlTemplate.Triggers>
- <Trigger Property="IsMouseOver" Value="True">
- <Setter TargetName="content" Property="RenderTransform" >
- <Setter.Value>
- <ScaleTransform ScaleX="1.1" ScaleY="1.1" />
- </Setter.Value>
- </Setter>
- </Trigger>
- <Trigger Property="IsPressed" Value="True">
- <Setter TargetName="content" Property="RenderTransform" >
- <Setter.Value>
- <ScaleTransform ScaleX=".95" ScaleY=".95" />
- </Setter.Value>
- </Setter>
- </Trigger>
- <Trigger Property="IsFocused" Value="True">
- </Trigger>
- </ControlTemplate.Triggers>
- </ControlTemplate>
- </Setter.Value>
- </Setter>
- </Style>
Pingback: Windows Client Developer Roundup 058 for 2/7/2011 - Pete Brown's 10rem.net
Pingback: Windows Client Developer Roundup 058 for 2/7/2011 · All About Computer
Hi! I’m very new to WPF. It would be nice if you also display the screenshot of the button using this style 🙂
Thank you for posting this! I was searching everywhere for this exact thing and couldn’t find anything until I ran across this. Thanks again!
Thankyou for this, just started with WPF and this was a great help.
It does have to be within Windows.Resources which took a novice like me some time to figure.
Hi, thank you! This worked for me.
You have a great blog here! Not only is the thinking unique, but also well laid out. Would like to read more from you.