1

When I put button on a toolbar, its style changes automatically to match the toolbar elements.

I derived my own class from a ToggleButton, and now if I instantiate one on the toolbar, it looks like ordinary button again. How can I access the built-in templates of the WPF, such that I don't have to recreate the whole template from scratch?

Spook
  • 25,318
  • 18
  • 90
  • 167
  • Waht is the purpose of extending the ToggleButton class ? Do you change the template ? – Novitchi S Nov 14 '12 at 08:08
  • http://stackoverflow.com/questions/2548863/how-can-i-prevent-a-togglebutton-from-being-toggled-without-setting-isenabled-fa Currently I changed the template to change its appearance, but I would rather not do so. – Spook Nov 14 '12 at 08:15

3 Answers3

2

Apparently the ToolBar has resources defined for some controls including Button and ToggleButton. This styles, you cand find them in ToolBar's metadata, are applied to each control of it's target type. So you can get the style and apply it yourself:

    <ToolBar>
        <my:CustomToggleButton 
            Height="100" Width="100" 
            Style="{StaticResource {x:Static ToolBar.ToggleButtonStyleKey}}" 
            Content="Press me!" />
    </ToolBar>

If you have a lot of button's and don't want to set the style for each one you could set the style in codebehind of your custom control.

Novitchi S
  • 3,711
  • 1
  • 31
  • 44
0

You can apply the style on your custom toggle button like this to give it a look of Toolbar toggle button -

<local:LockableToggleButton LockToggle="True" Content="Test"
       Style="{StaticResource {x:Static ToolBar.ToggleButtonStyleKey}}"/>
Rohit Vats
  • 79,502
  • 12
  • 161
  • 185
0

You can access an change the template in the OnApplyTemplate override using the VisualTreeHelper and/ or LogicalTreeHeler.

bitbonk
  • 48,890
  • 37
  • 186
  • 278