首页 > 解决方案 > 如何更改 WPF 工具栏下拉按钮的背景颜色?

问题描述

如何更改 WPF 工具栏下拉按钮的背景颜色?

更新:

在此处输入图像描述

XAML 代码:

<Window x:Class="WpfTutorialSamples.Common_interface_controls.ToolbarSample"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="ToolbarSample" Height="200" Width="300">
     <DockPanel>
        <ToolBarTray DockPanel.Dock="Top">
            <ToolBar>
                <Button Content="New" />
                <Button Content="Open" />
                <Button Content="Save" />
            </ToolBar>
            <ToolBar Background="GreenYellow">
                <Button Content="Cut" />
                <Button Content="Copy" />
                <Button Content="Paste" />
            </ToolBar>
        </ToolBarTray>
        <TextBox Text="Test" />
    </DockPanel>
 </Window>

标签: wpfxamltoolbardropdownbutton

解决方案


用户可以在 ToolBarAdv 中自定义溢出按钮颜色。可以通过在 ToolBarAdv 中编辑 ToggleButton Style 来实现。下面的代码演示了相同的。

<Style TargetType="{x:Type ToggleButton}">



<!--Set Overflow Button backcolor -->



<Setter Property="Background" Value="Yellow" />



<Setter Property="BorderBrush" Value="Transparent"/>



<Setter Property="BorderThickness" Value="1"/>



<Setter Property="Foreground" Value="{DynamicResource {x:Static



SystemColors.ControlTextBrushKey}}"/>



<Setter Property="HorizontalContentAlignment" Value="Center"/>



<Setter Property="VerticalContentAlignment" Value="Stretch" />



<Setter Property="Padding" Value="1"/>



<Setter Property="Template">



<Setter.Value>



<ControlTemplate TargetType="{x:Type ToggleButton}">



<Border x:Name="Chrome" Background="{TemplateBinding Background}"



BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="1" CornerRadius="0,3,3,0">



<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"



Margin="{TemplateBinding Padding}" RecognizesAccessKey="True"



SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"



VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>



</Border>



<ControlTemplate.Triggers>



<Trigger Property="IsEnabled" Value="false">





<Setter Property="Opacity" Value="0.3"/>



</Trigger>



</ControlTemplate.Triggers>



</ControlTemplate>



</Setter.Value>



</Setter>



</Style>

推荐阅读