【发布时间】:2010-05-04 11:59:01
【问题描述】:
有问题。当我更改 WPF 工具栏的背景颜色时,右上角的溢出按钮不会改变颜色。如何解决?
【问题讨论】:
-
我是 WPF 的新手。 antongladchenko,你能贴出你是怎么做的代码吗?几行代码对于快速入门会更好。谢谢!
标签: .net wpf button overflow toolbar
有问题。当我更改 WPF 工具栏的背景颜色时,右上角的溢出按钮不会改变颜色。如何解决?
【问题讨论】:
标签: .net wpf button overflow toolbar
不幸的是,溢出按钮有一个固定的背景。更准确地说,它在默认模板中设置为静态值。如果您想获得它们的副本,请参阅 this MSDN forum thread 或 MSDN。或This tool from Chris Sells
在模板中,您会看到一个 ToggleButton,用于显示/隐藏溢出面板。这是要更改以产生您正在寻找的效果的那个。
因此,您的问题的答案是,您需要在 XAML 中包含工具栏的完整样式,并将按钮的背景更改为与工具栏的其余部分相同。
【讨论】:
我遇到了你上面描述的同样的问题。我的解决方案如下:
using System.Windows.Controls.Primitives;
using System.Windows.Media;
namespace WPF.Controls
{
public class ToolBar : System.Windows.Controls.ToolBar
{
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
var overflowPanel = base.GetTemplateChild("PART_ToolBarOverflowPanel") as ToolBarOverflowPanel;
if (overflowPanel != null)
{
overflowPanel.Background = OverflowPanelBackground ?? Background;
overflowPanel.Margin = new Thickness(0);
}
}
public Brush OverflowPanelBackground
{
get;
set;
}
}
}
XAML 示例:
<Window
x:Class="WPF.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:src="clr-namespace:WPF.Controls">
<ToolBarTray Background="White">
<wpf:ToolBar Background="Pink" OverflowPanelBackground="Peru" Band="1" BandIndex="1" Width="50">
<Button Content="Cut" />
<Button Content="Copy" />
<Button Content="Paste" />
</wpf:ToolBar>
<wpf:ToolBar Background="Aqua" Band="2" BandIndex="1" Width="70">
<Button Content="Undo" />
<Button Content="Redo" />
</wpf:ToolBar>
<wpf:ToolBar OverflowPanelBackground="Yellow" Band="2" BandIndex="2" Width="100">
<Button Content="Paint"/>
<Button Content="Spell"/>
<Separator/>
<Button Content="Save"/>
<Button Content="Open"/>
</wpf:ToolBar>
</ToolBarTray>
</Window>
【讨论】:
Alex 的回答很好。另一种修改OverflowButton和OverflowPanel颜色的方法是在loaded事件中修改。
XAML
<ToolBar Loaded="ToolBar_Loaded">
后面的代码:
private void ToolBar_Loaded(object sender, RoutedEventArgs e)
{
ToolBar toolBar = sender as ToolBar;
var overflowGrid = toolBar.Template.FindName("OverflowGrid", toolBar) as Grid;
if (overflowGrid != null)
{
overflowGrid.Background = Brushes.Red;
}
var overflowButton = toolBar.Template.FindName("OverflowButton", toolBar) as ToggleButton;
if (overflowButton != null)
{
overflowButton.Background = Brushes.Red;
}
var overflowPanel = toolBar.Template.FindName("PART_ToolBarOverflowPanel", toolBar) as ToolBarOverflowPanel;
if (overflowPanel != null)
{
overflowPanel.Background = Brushes.Red;
}
}
名称(OverflowGrid、OverflowButton 和 PART_ToolBarOverflowPanel)可以在默认控件模板中找到,该模板可以从 WPF 的 GitHub 页面下载。
【讨论】: