tags:

views:

345

answers:

1

I have read in two different books that in WPF, the ToolBar.Header property doesn't do anything:

However, I'm creating my ToolBar objects dynamically like this (tbtToolBar is actually a ToolBarTray defined in the Xaml, vm is the window's ViewModel):

foreach (IToolBarViewModel toolBarViewModel in vm.ToolBars)
{
    ToolBar toolBar = new ToolBar();
    toolBar.DataContext = toolBarViewModel;

    // Bind the Header Property
    Binding headerBinding = new Binding("Header");
    toolBar.SetBinding(ToolBar.HeaderProperty, headerBinding);

    // Bind the Items Property
    Binding itemsBinding = new Binding("Items");
    toolBar.SetBinding(ToolBar.ItemsSourceProperty, itemsBinding);

    tbtToolBar.ToolBars.Add(toolBar);
}

And the Header property clearly shows up in a label as the first item in the toolbar. This is not the behavior I want. I would like to use the Header as a title in a drop-down list of ToolBars when the user right clicks on the ToolBarTray, just like the books describe.

So, I tried to get rid of the Header by setting:

toolBar.HeaderTemplate = new DataTemplate();

This works, but now there's a small unsightly gap in the toolbar.

  1. Is there a way to make the header invisible without the gap?
  2. Why are the books clearly wrong? Did something change between then and now?
A: 

The only way I was able to make this work was to keep the Header property null, and create another property on the ToolBarViewModel called Name.

Scott Whitlock