Making the ToolBar Button Style apply to UserControls in the Toolbar Making the ToolBar Button Style apply to UserControls in the Toolbar wpf wpf

Making the ToolBar Button Style apply to UserControls in the Toolbar


Ooh, my apologies! I misinterpreted your question. Give this a shot:

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

The style only targets buttons, so it can only be applied to buttons. (Which won't be a problem by the look of your comment.)


I found a way, that seems to work, although I'm interested in other ways of solving this.

I added a loaded event handler to my UserControl and put this inside it.

if (button.Style == null && this.Parent is ToolBar){    button.Style = (Style)FindResource(ToolBar.ButtonStyleKey);}

Where button is the name of the button inside my UserControl.


You would require some powerful black magic in order to get it to create a functioning new style for your usercontrol automatically. There are built-in styles for each type of built-in control that you may add to the toolbar. (Notice that ToolBar.ButtonStyleKey is not alone.) You'll need to craft and apply your own style that matches the standard toolbar elements yourself.