Setting Button FlatStyle in WPF
Asked Answered
K

2

63

I have just been learning about how styles and control templates in WPF can affect the appearance of buttons,

I'm trying to set the Button's FlatStyle, in the resources I've seen I can't find anything that tells me how I can do this, in Windows Forms this is set through FlatStyle = Flat.

How would one do this in WPF?

Kirovabad answered 30/3, 2009 at 14:10 Comment(1)
Does this answer your question? wpf flat buttonDab
L
148

The ToolBar class defines a Style that makes Buttons look flat. An example of using it is:

<Button Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}"/>

This also works for a ToggleButton when using ToggleButtonStyleKey.

WPF lets you completely restyle controls to make them look like whatever you want, which is why it doesn't have such a specific FlatStyle property on the Button control.

Litigation answered 30/3, 2009 at 14:14 Comment(5)
Which is more correct? To create a style for Button that mimics the ToolBar.ButtonStyle, or just use the code you've provided. Thanks EdKirovabad
Depends on your scenario I think. You can also "derive" a style from the ToolBar Button style by using the Style.BasedOn property.Litigation
In a pinch, this works pretty well. It is nice to use for a WPF Button control with an image as the sole content, as the image now will not shift up one pixel when you click and hold on the button. Thanks Kent.Outsert
That's a good start. Unfortunately, WPF's toolbar button seems to show being focused in a way that can easily be confused with a "checked" toggle button (in fact, it looks a lot like a graphics bug, where the hover state is not correctly reset upon leaving the control) :-/Targum
Just noticed - setting the button's Focusable property to false will fix that, though. (Note that the button should still be reachable with keyboard shortcuts - it's just not included in the tab order, which is generally not something expected of a toolbar button.)Targum
J
25

Add the following to your Window/Page resources:

<Style BasedOn="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" TargetType="Button"></Style>

It will apply the flat style to all buttons in that styles scope.

Just answered 5/10, 2012 at 10:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.