在WPF中,如何使ToolBar.Header消失?

时间:2009-05-18 14:53:56

标签: wpf toolbar

我已阅读两本不同的书籍,在WPF中,ToolBar.Header属性不执行任何操作:

但是,我正在动态创建我的ToolBar对象(tbtToolBar实际上是Xaml中定义的ToolBarTray,vm是窗口的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);
}

Header属性清晰地显示在标签中,作为工具栏中的第一项。这不是我想要的行为。当用户右键单击ToolBarTray时,我想在工具栏的下拉列表中使用标题作为标题,就像书中描述的那样。

所以,我试图通过设置来删除标题:

toolBar.HeaderTemplate = new DataTemplate();

这样可行,但现在工具栏中有一个小的难看的间隙。

  1. 有没有办法让标题在没有间隙的情况下隐藏?
  2. 为什么这些书明显错了?当时和现在之间有什么变化吗?

1 个答案:

答案 0 :(得分:0)

我能够完成这项工作的唯一方法是将Header属性保持为null,并在名为Name的ToolBarViewModel上创建另一个属性。

相关问题