sudarsanyes
sudarsanyes

Reputation: 3204

WPF Toolbar removing custom styles for controls placed within it

I have defined generic themes (key less, styles based on types), for buttons and text boxes. When I create a button instance in my Window where these resources are merged, the styles are rendered properly. But for the same button hosted within a ToolBar, the styles are not applied; the style reverts back to Windows default. Any idea why?

P.S: If I set the style of the buttons hosted within the ToolBar explicitly, then it works fine, but this is something I do not want to do.

This works:

            <ToolBarTray>
                <ToolBar Band="1"
                         BandIndex="1">
                    <Button Content="Add" Style="{DynamicResource ResourceKey={x:Type Button}}" />
                </ToolBar>
            </ToolBarTray>

enter image description here

This does not work:

            <ToolBarTray>
                <ToolBar Band="1"
                         BandIndex="1">
                    <Button Content="Add" />
                </ToolBar>
            </ToolBarTray>

enter image description here

In both the cases, if the button is placed outside the ToolBar, styles are applied properly!

Upvotes: 3

Views: 1129

Answers (1)

Chris W.
Chris W.

Reputation: 23280

So if we go look at a style template for your toolbar we find this in there.

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

Which is explicitly defining and overriding your embedded Button's with another style nested in the template.

You can either alter or remove this from the template, or override it at the instance level where you would just change the BasedOn value to point to your own Style template. There's also tutorials out on the net for styling the toolbar for more detailed information. Hope this helps.

Upvotes: 3

Related Questions