WPF 工具栏中 OverFlowButton 的颜色

发布于 2024-08-31 05:38:54 字数 170 浏览 3 评论 0原文

有问题。当我更改 WPF 工具栏的背景颜色时,右上角的溢出按钮不会改变颜色。如何修复它?

例子: 替代文本 http://biztimes.ru/toolbar.jpg

There was a problem. When I change the background color of WPF toolbar Overflow Button in the right corner does not change color. How to fix it?

Example:
alt text http://biztimes.ru/toolbar.jpg

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

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

发布评论

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

评论(4

墨离汐 2024-09-07 05:38:54

不幸的是,溢出按钮有固定的背景。更准确地说,它在默认模板中设置为静态值。请参阅此 MSDN 论坛主题MSDN(如果您想获取他们。或者 Chris Sells 提供的这个工具

在模板中,您会看到一个 ToggleButton,它用于显示/隐藏溢出面板。需要更改这一点才能达到您想要的效果。

因此,您的问题的答案是您需要在 XAML 中包含工具栏的完整样式,并将按钮的背景更改为与工具栏的其余部分相同。

The overflow button unfortunately has a fixed background. More precisely, it is set to a static value in the default template. See this MSDN forum thread or MSDN if you want to get a copy of them. Or This tool from Chris Sells

In the template, you'll see a ToggleButton, that is used to show/hide the overflow panel. This is the one to change to have the effect that you are looking for.

So, the answer to your question is that you need to include the full style of the toolbar in your XAML and change the background of the button to be the same as the rest of the toolbar.

孤单情人 2024-09-07 05:38:54

我遇到了你上面描述的同样的问题。我的解决方案如下:

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>

I had the same problem you described above. My solution is the following:

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 sample:

<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>
春花秋月 2024-09-07 05:38:54

亚历克斯的回答很好。修改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 页面下载。

Alex's answer is good. Another way to modify the color of the OverflowButton and OverflowPanel is to modify them in the loaded event.

XAML

<ToolBar Loaded="ToolBar_Loaded">

Code behind:

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

The names (OverflowGrid, OverflowButton, and PART_ToolBarOverflowPanel) can be found in the default control template, which could be downloaded from WPF's GitHub page.

笑脸一如从前 2024-09-07 05:38:54

Alex Padabed 的答案很接近,但根据 OP 错过了下拉按钮。您可以通过添加以下内容来解决此问题:

public Brush OverflowButtonBackground { get; set; }

protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
{
    // as per Alex's answer

    ToggleButton overflowButton = this.Template.FindName("OverflowButton", this) as ToggleButton;
    if ( overflowButton != null)
    {
        overflowButton.Background = OverflowButtonBackground ?? Background;
    }
}

Alex Padabed's answer was close, but missed the dropdown button as per the OP. You can remedy this by adding:

public Brush OverflowButtonBackground { get; set; }

protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
{
    // as per Alex's answer

    ToggleButton overflowButton = this.Template.FindName("OverflowButton", this) as ToggleButton;
    if ( overflowButton != null)
    {
        overflowButton.Background = OverflowButtonBackground ?? Background;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文