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

发布于 2024-07-21 00:29:28 字数 1120 浏览 6 评论 0原文

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

  • Adam Nathan 所著的《Windows Presenter Foundation Unleashed》,第 12 页。 第119
  • 章hl=en&ei=xnMRSpv5GpfEMtvl7KYG&sa=X&oi=book_result&ct=result&resnum=10" rel="nofollow noreferrer">Pro WPF 与 VB 2008,第 15 页 章

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);
}

650 工具栏。 这不是我想要的行为。 当用户右键单击 ToolBarTray 时,我想使用标题作为 ToolBar 下拉列表中的标题,就像书籍所描述的那样。

因此,我尝试通过设置来摆脱标题:

toolBar.HeaderTemplate = new DataTemplate();

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

  1. 有没有办法让标题不可见且没有间隙?
  2. 为什么这些书明显是错误的? 从那时到现在有什么变化吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷…雨湿花 2024-07-28 00:29:28

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文