Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change shape of WPF button without changing other styles

Tags:

.net

wpf

I would like to change the shape of a WPF button from the default round rectangle to another shape (say, an arrow), but I want to keep the rest of the styling -- fill color, border color, hover states, etc. I want the button to have the same styles as all the other regular buttons (which I believe depends on the version of Windows, system theme colors, etc. so it's not like I can recreate the button style from scratch and hard code the right colors).

Is there some way to do that?

like image 280
Hank Avatar asked Feb 02 '12 19:02

Hank


People also ask

How do I change the shape of a button in WPF?

The button's shape can be changed to any shape by applying the control template to a button.

How do you round button edges in WPF?

You can achieve rounded corners on a button without having to write any XAML (other than a Style, once) and without having to replace the template or set/change any other properties. Just use an EventSetter in your style for the button's "Loaded" event and change it in code-behind.

What is toggle button in WPF?

Toggle Button is known as a control with the help of which we can move from one state to another state. CheckBox and RadioButton are an example of the Toggle Button.


1 Answers

Make a copy of the default Button Template, and simply change the default panel to your own custom shape.

In the case of the button, it looks like you need to replace the Border with something like a Path defining an Arrow.

Since a Path doesn't have content, you'll probably have to put both the shape and the content in another panel which allows objects to overlap, such as a Grid.

So your end result will look something like this

<Grid>
    <Path />
    <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
</Grid>

instead of the default

<Button>
    <ContentPresenter />
</Button>
like image 109
Rachel Avatar answered Nov 10 '22 01:11

Rachel