DCSIMG
How to bind ToolBar items - Essential WPF

How to bind ToolBar items

You have created a view-model for your ToolBar control, and you have a collection of commands exposed by the view-model as a property (Commands). Now you want to bind your ToolBar.ItemsSource with that property, so you have something like this:

<ToolBar ItemsSource="{Binding Commands}" Height="64" />

 

Of course, you want to have each command as a button, so you’ve created a DataTemplate:

<DataTemplate DataType="{x:Type local:CommandModel}">
    <Button Command="{Binding Command}">
        <StackPanel>
            <Image Source="{Binding Icon}" Width="32" Height="32" />
            <TextBlock Text="{Binding Title}" VerticalAlignment="Center" />
        </StackPanel>
    </Button>
</DataTemplate>

Gluing all together you end up like this:

image

Now you’re asking yourself, why does the toolbar buttons looks totally different comparing to toolbar without binding?

image

The answer to this question, like other weirdo things in WPF, hidden inside the control’s code.

Looking with Reflector at ToolBar.PrepareContainerForItemOverride, you find this:

image

What happens here is that the toolbar picks a well known style-key based on the element type (the container) and not the item type (our CommandModel). For example, if the item added to the toolbar is of type Button, the toolbar picks the ButtonStyleKey, and set the button default-style-key with that key. This is how a button added directly to the toolbar gets a special style.

Using data binding, we are not adding Buttons… we are actually adding CommandModel instances, so the toolbar ignores it, ‘name’ stays null and nothings special is really happens, so our DataTemplate’s button stays with its normal style.

To overcome this issue, we simply set our button style explicitly with the ToolBar’s ready-made button style like this:

<DataTemplate DataType="{x:Type local:CommandModel}">
    <Button Command="{Binding Command}"
            Style="{DynamicResource ResourceKey={x:Static ToolBar.ButtonStyleKey}}">
        <StackPanel>
            <Image Source="{Binding Icon}" Width="32" Height="32" />
            <TextBlock Text="{Binding Title}" VerticalAlignment="Center" />
        </StackPanel>
    </Button>
</DataTemplate>

 

That's it, now our data-bound toolbar works as expected.

Download the code from here.

Published Thursday, July 22, 2010 11:37 PM by Tomer Shamam

Comments

# re: How to bind ToolBar items

Thursday, June 16, 2011 3:25 PM by ojeswini

Sir,

I am not created to Data Template through expression blend 4 . Please help me

# re: How to bind ToolBar items

Friday, June 17, 2011 9:58 AM by Tomer Shamam

Hi ojeswini,

Help with what? please be more specific.

# re: How to bind ToolBar items

Friday, June 17, 2011 2:49 PM by Debasish

Hello Tomer

I need Help, how to add seperator between two buttons?

# re: How to bind ToolBar items

Sunday, June 19, 2011 11:06 AM by Tomer Shamam

You should have a different type for that, or additional property in CommandModel, such as IsSeparator. Then you should use a different DataTemplate or use DataTemplateSelector based on the new property accordantly.

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above:
Powered by Community Server (Commercial Edition), by Telligent Systems