tags:

views:

480

answers:

2

I'd like to host custom items in a ToolBar in an ItemsControl. However, the buttons I add are rendered below the toolbar and as regular buttons rather than in the ToolBar with the ToolBar look and feel.

This can be reproduced with a few lines of Xaml (I've excluded the default content). The custom ItemsControl:

<ToolBar ItemsSource="{Binding Items}" />

The example window:

<StackPanel Orientation="Vertical">
    <local:UserControl1>
        <Button>control button</Button>
    </local:UserControl1>
    <Button>standard button</Button>
    <ToolBar>
        <Button>window toolbar button</Button>
    </ToolBar>

I would expect that the "control button" would render similarly to the "window toolbar button", rather it renders like the "standard button" below an empty ToolBar.

Any guidance will be appreciated.

+2  A: 

set the style on the button like this:

Style="{DynamicResource {x:Static ToolBar.ButtonStyleKey}}"
Micah
This gets the style right, but the button is still placed below the toolbar rather than in it.
dmo
Wow, that tip saved me a lot of work. I wanted to add a group of toolbar buttons bound to a data source, so I was embedding an ItemsControl in my toolbar, with a horizontal stack panel, and each item templated as a button, but they weren't appearing as normal toolbar buttons until I added this style.
Anthony Brien
A: 

I ended up getting the behavior I wanted by naming the custom ToolBar, then adding a property in the code-behind that could be used by consumers of the control to add items through code.

dmo