WPF 工具栏:如何删除夹点和溢出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1050953/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF ToolBar: how to remove grip and overflow
提问by Tom
In a nested WPF ToolBarPanel-ToolBar-Menu we want to get rid of the grip handle to the left and the overflow area to the right. they are both grayed out, but we'd like them to not be displayed at all.
在嵌套的 WPF ToolBarPanel-ToolBar-Menu 中,我们希望摆脱左侧的手柄和右侧的溢出区域。它们都变灰了,但我们希望它们根本不显示。
any ideas on how to accomplish that?
关于如何实现这一点的任何想法?
just in case my terms aren't entirely correct, if you look at the image in Figure 3 of the link below, on the lowest of the three toolbars there's the grip to the left of the dropdown and to the right of the right-most button there's the overflow.
以防万一我的术语不完全正确,如果您查看下面链接的图 3 中的图像,在三个工具栏的最低处,下拉菜单的左侧和最右侧的右侧都有手柄按钮有溢出。
回答by rmoore
The grip can be removed by setting the attached property ToolBarTray.IsLocked="True"
on the ToolBar.
可以通过ToolBarTray.IsLocked="True"
在 ToolBar 上设置附加属性来移除夹点。
To remove the Overflow ToggleButton, you will have to remove it in a custom ControlTemplate as sixlettervariables suggests, which if you have blend or can download the Blend 3 Preview is not overly difficult.
要删除Overflow ToggleButton,您必须按照 Sixlettervariables 的建议在自定义 ControlTemplate 中删除它,如果您有 blend 或可以下载 Blend 3 Preview 并不太困难。
You could also just hide the button in the loaded event of the ToolBar, though whichever route you take, you should also set the attached property ToolBar.OverflowMode="Never"
on the ToolBar's menu, so that items cannot accidentally overflow into an unreachable area.
您也可以在 ToolBar 的加载事件中隐藏按钮,无论您采用哪种方式,您还应该ToolBar.OverflowMode="Never"
在 ToolBar 的菜单上设置附加属性,这样项目就不会意外溢出到无法访问的区域。
<ToolBarPanel DockPanel.Dock="Top">
<ToolBar ToolBarTray.IsLocked="True" Loaded="ToolBar_Loaded">
<Menu ToolBar.OverflowMode="Never">
<MenuItem Header="File" />
<MenuItem Header="New" />
</Menu>
</ToolBar>
</ToolBarPanel>
And set the Overflow ToggleButton to collapsed:
并将溢出切换按钮设置为折叠:
private void ToolBar_Loaded(object sender, RoutedEventArgs e)
{
ToolBar toolBar = sender as ToolBar;
var overflowGrid = toolBar.Template.FindName("OverflowGrid", toolBar) as FrameworkElement;
if (overflowGrid != null)
{
overflowGrid.Visibility = Visibility.Collapsed;
}
var mainPanelBorder = toolBar.Template.FindName("MainPanelBorder", toolBar) as FrameworkElement;
if (mainPanelBorder != null)
{
mainPanelBorder.Margin = new Thickness();
}
}
回答by user7116
You can use Blend to rather simply override the ControlTemplatefor the ToolBarPanel, Menu, or ToolBar.
您可以使用 Blend 来简单地覆盖ToolBarPanel、Menu 或 ToolBar的 ControlTemplate。
- Right click on the ToolBar and select Edit Template
- From Edit Template, select Edit a Copy
- I recommend adding the copy to a Resource Dictionary
- Click Ok
- 右键单击工具栏并选择编辑模板
- 从“编辑模板”中,选择“编辑副本”
- 我建议将副本添加到资源字典
- 单击确定
You'll now be editing the control template for the ToolBarPanel, and can set the visibility to Collapsed for the grip and overflow signal. You can rinse and repeat for the other controls. It is a bit time consuming, but isn't terribly hard with Blend.
您现在将编辑 ToolBarPanel 的控件模板,并且可以将夹点和溢出信号的可见性设置为折叠。您可以冲洗并重复其他控件。这有点耗时,但对于 Blend 来说并不难。
回答by John Fisher
You can "remove" the overflow without supplying a new control template by setting the ToolBar
to have negative right margins (and throw in a negative left margin so it doesn't look odd with rounded left edges but square right edges). Then, add ClipToBounds="True"
to the ToolBarPanel
which will cut off the edges of the toolbar which are now sticking outside the panel's area.
您可以通过将 设置ToolBar
为具有负的右边距来“删除”溢出,而无需提供新的控制模板(并加入负的左边距,这样它的左边缘圆滑但右边缘看起来并不奇怪)。然后,添加ClipToBounds="True"
到ToolBarPanel
这将切断工具栏的边缘,这些边缘现在粘在面板区域之外。
<ToolBarPanel Grid.Row="0" ClipToBounds="True">
<ToolBar ToolBarTray.IsLocked="True" Margin="-5,0,-13,0" Padding="5,0,0,0">
. . .
回答by Thomas Levesque
Rather than hiding the overflow button completely, I think it's better to show it only when necessary. This can be done by binding its Visibility
property to its IsEnabled
property:
与其完全隐藏溢出按钮,我认为最好仅在必要时显示它。这可以通过将其Visibility
属性绑定到其IsEnabled
属性来完成:
private static void FixupToolBarOverflowArrow(ToolBar toolBar)
{
Action fixup = () =>
{
var overflowButton = toolBar.Template.FindName("OverflowButton", toolBar) as ButtonBase;
if (overflowButton != null)
{
overflowButton.SetBinding(
VisibilityProperty,
new Binding("IsEnabled")
{
RelativeSource = RelativeSource.Self,
Converter = new BooleanToVisibilityConverter()
});
}
};
if (toolBar.IsLoaded)
{
fixup();
}
else
{
RoutedEventHandler handler = null;
handler = (sender, e) =>
{
fixup();
toolBar.Loaded -= handler;
};
toolBar.Loaded += handler;
}
}
(the same thing can be done in XAML by redefining the template)
(同样的事情可以通过重新定义模板在 XAML 中完成)
回答by Belmiris
I am just starting out with WPF and could not get any of the above methods to hide my overflow arrow (Visual Studio 2010).The only thing that seemed to affect the arrow was the Toolbar_Load example above but all that did was turn the arrow into an empty space that looked as bad as the arrow. The easiest way I could figure was just to set the margins of the toolbar.
我刚开始使用 WPF,无法使用上述任何方法来隐藏我的溢出箭头(Visual Studio 2010)。似乎影响箭头的唯一事情是上面的 Toolbar_Load 示例,但所做的只是将箭头变成一个看起来像箭头一样糟糕的空白空间。我能想到的最简单的方法就是设置工具栏的边距。
<ToolBar Height="26"
Name="toolBar"
DockPanel.Dock="Top"
ToolBarTray.IsLocked="True"
ToolBar.OverflowMode="Never" <!-- no effect -->
Margin="0,0,-13,0"> <!-- worked -->
<Menu ToolBar.OverflowMode="Never"> <!-- no affect -->
<MenuItem Header="_File"></MenuItem>
</Menu>
</ToolBar>
回答by frediano
The methods above work to hide the overflow; I've used the following to hide the gripper:
上述方法可以隐藏溢出;我使用以下内容来隐藏抓手:
<Label Height="44" Width="30" Background="{StaticResource CtrlBackground}" Margin="-20,0,0,0"></Label>
for a Horizontal layout, and
对于水平布局,和
<Label Height="44" Width="230" Background="{StaticResource CtrlBackground}" Margin="0,-20,0,0" HorizontalAlignment="Left"></Label>
for a Vertical layout. Place the above after the Toolbar(or ToolbarTray, if using that)
对于垂直布局。将上述内容放在工具栏(或工具栏托盘,如果使用的话)之后
Use whatever Width and Height is needed for your buttons.
使用按钮所需的任何宽度和高度。
Kaxaml is excellent for playing with this stuff.
Kaxaml 非常适合玩这些东西。