C# 是否可以更改WPF中禁用按钮的背景色?

C# 是否可以更改WPF中禁用按钮的背景色?,c#,wpf,xaml,C#,Wpf,Xaml,我试图获得“使整个窗口及其所有控件变暗”的效果 窗口及其上的所有内容也需要禁用 问题是,当按钮被禁用时,它似乎不允许您更改背景色 在WPF中是否有方法更改按钮的背景色,即使按钮已禁用? XAML: <Window x:Class="TestDimWindows.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft

我试图获得“使整个窗口及其所有控件变暗”的效果

窗口及其上的所有内容也需要禁用

问题是,当按钮被禁用时,它似乎不允许您更改背景色

在WPF中是否有方法更改按钮的背景色,即使按钮已禁用?

XAML:

<Window x:Class="TestDimWindows.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">

    <Grid x:Name="dimElement">
        <StackPanel HorizontalAlignment="Left">
            <TextBlock 
                Text="This is an example of dimming a window." 
                Margin="5"/>
            <StackPanel 
                HorizontalAlignment="Left" 
                Margin="5">
                <Button x:Name="theButton" 
                        Content="Dim the window" 
                        Click="Button_Click"/>
            </StackPanel>
        </StackPanel>
    </Grid>

</Window>
using System.Windows;
using System.Windows.Media;

namespace TestDimWindows
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            dimElement.Background = new SolidColorBrush(Colors.Gray);
            dimElement.Opacity = 0.5;
            dimElement.IsEnabled = false;

            //I want this button to look "dimmed out" as well
            //but since it is disabled, it is a ghostly white.
            //how can I change the color even though it is disabled?
            theButton.Background = new SolidColorBrush(Colors.Gray);
        }
    }
}

您可以为其创建自己的控件模板

我建议使用Blend(如果您没有许可证,可以获得试用版)来创建您当前使用的模板的副本


如果检查当前模板,它必须为某个位置设置禁用的背景色。根据IsEnabled属性查找触发器。

您可以删除onclick,更改颜色,并使其处于“禁用”状态。

查看VisualBrush。您可以将VisualBrush的视觉表示设置为控件,VisualBrush将重新创建控件的视觉表示,而不具有任何实际功能

我从Sales/Griffiths的“编程WPF”(关于图形的第13章)中选取了这个示例,并自己对其进行了一些修改,然后再进一步为您演示一个解决方案

这样做的目的是创建一个简单的绘图界面(输入两个点的x和y坐标以在它们之间画一条线),但也反映了下面的图像。它并不意味着健壮,但它应该展示我认为您需要的功能。
VisualBrush和SolidColorBrush的最后两个矩形元素是如何创建复制控件,然后将其着色

你所能做的就是将这两个元素覆盖在你想要遮光的页面/窗口/等等上,然后在你想要效果发生时使它们可见

希望这有帮助

<Window x:Class="GraphicsTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Window.Resources>
</Window.Resources>
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="2*"/>
        <RowDefinition Height="1*"></RowDefinition>
    </Grid.RowDefinitions>
    <Grid x:Name="mainUI">
        <DockPanel>
            <GroupBox Header="Point 1" Name="gbPoint1" DockPanel.Dock="Top">
                <StackPanel Orientation="Horizontal">
                    <Label Width="40" HorizontalContentAlignment="Right">X:</Label>
                    <TextBox Name="tbX1" Width="40" TextChanged="Content_TextChanged"/>
                    <Label Width="40" HorizontalContentAlignment="Right">Y:</Label>
                    <TextBox Name="tbY1" Width="40" TextChanged="Content_TextChanged"/>
                </StackPanel>
            </GroupBox>
            <GroupBox Header="Point 2" Name="gbPoint2"  DockPanel.Dock="Top">
                <StackPanel Orientation="Horizontal">
                    <Label Width="40" HorizontalContentAlignment="Right">X:</Label>
                    <TextBox Name="tbX2" Width="40" TextChanged="Content_TextChanged"/>
                    <Label Width="40" HorizontalContentAlignment="Right">Y:</Label>
                    <TextBox Name="tbY2" Width="40" TextChanged="Content_TextChanged"/>
                </StackPanel>
            </GroupBox>
            <Canvas>
                <Line Name="lnDraw" Stroke="Black" StrokeThickness="2"/>
            </Canvas>
        </DockPanel>
    </Grid>
    <Rectangle Grid.Row="1">
        <Rectangle.LayoutTransform>
            <ScaleTransform ScaleY="-1"/>
        </Rectangle.LayoutTransform>
        <Rectangle.Fill>
            <VisualBrush Visual="{Binding ElementName=mainUI}" />
        </Rectangle.Fill>
    </Rectangle>
    <Rectangle Grid.Row="1">
        <Rectangle.Fill>
            <SolidColorBrush Color="Black" Opacity=".5"/>
        </Rectangle.Fill>
    </Rectangle>
</Grid>

我会尝试用一个填充整个网格的矩形来进行暗显效果,该矩形是灰色的,不透明度小于1,z索引高于正常控件。默认情况下,矩形的visibility=collapsed,然后当某个适当的“IsEnabled”属性变为“true”时,我将使用触发器将其visibility设置为visible

public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();
    }

    private void Content_TextChanged(object sender, TextChangedEventArgs e)
    {
        int x1;
        int x2;
        int y1;
        int y2;

        if (int.TryParse(tbX1.Text, out x1) && int.TryParse(tbX2.Text, out x2) && int.TryParse(tbY1.Text, out y1) && int.TryParse(tbY2.Text, out y2))
        {
            lnDraw.X1 = x1;
            lnDraw.X2 = x2;
            lnDraw.Y1 = y1;
            lnDraw.Y2 = y2;
        }
    }
}