在WPF中,如何让ToolBar.Header消失?
我在两本不同的书中读到,在 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();
这可行,但现在工具栏中有一个难看的小间隙。
- 有没有办法让标题不可见且没有间隙?
- 为什么这些书明显是错误的? 从那时到现在有什么变化吗?
I have read in two different books that in WPF, the ToolBar.Header property doesn't do anything:
- Windows Presentation Foundation Unleashed by Adam Nathan, pg. 119
- Pro WPF with VB 2008, pg. 650
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.
- Is there a way to make the header invisible without the gap?
- Why are the books clearly wrong? Did something change between then and now?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我能够完成这项工作的唯一方法是将 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.