有一个问题。当我更改WPF工具栏的背景颜色右上角的溢出按钮不会改变颜色。如何解决?
答案 0 :(得分:4)
不幸的是,溢出按钮具有固定的背景。更确切地说,它在默认模板中设置为静态值。如果您想获得它们的副本,请参阅this MSDN forum thread或MSDN。或This tool from Chris Sells
在模板中,您将看到一个ToggleButton,用于显示/隐藏溢出面板。这是要改变以获得您正在寻找的效果的那个。
因此,您的问题的答案是您需要在XAML中包含工具栏的完整样式,并将按钮的背景更改为与工具栏的其余部分相同。
答案 1 :(得分:1)
我遇到了你上面描述的同样的问题。我的解决方案如下:
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>
答案 2 :(得分:0)
亚历克斯的答案很好。修改OverflowButton和OverflowPanel颜色的另一种方法是在加载的事件中对其进行修改。
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页面下载。