简体   繁体   English

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

[英]Is it possible to change the background color of a disabled button in WPF?

I am trying to get the effect of "dimming out the whole window and all controls on it". 我试图获得“调暗整个窗口及其所有控件”的效果。

The window and everything on it also needs to be disabled. 窗口及其上的所有内容也需要禁用。

The problem is that when button is disabled, it doesn't seem to let you change the Background color. 问题是,当禁用按钮时,它似乎不允许您更改背景颜色。

Is there a way in WPF to change the background color of a button even though it is disabled? 有没有一种方法在WPF中更改按钮的背景颜色,即使它被禁用?

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

Code Behind: 代码背后:

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

您可以删除onclick,然后更改颜色,并使其成为“禁用”状态。

You can create your own control template for it. 您可以为它创建自己的控件模板。

I would suggest using Blend (you can get a trial edition if you don't have a license) to create a copy of the template you are currently using. 我建议使用Blend(如果您没有许可证,可以获得试用版)来创建您当前使用的模板的副本。

If you examine the current template, it must be setting the background color for disabled somewhere. 如果检查当前模板,则必须在某处设置禁用的背景颜色。 Look for a trigger based on the IsEnabled property. 根据IsEnabled属性查找触发器。

Look into the VisualBrush. 查看VisualBrush。 You can set the visual of a VisualBrush to a control, and the VisualBrush will recreate a visual representation of a control, without any of the actual functionality. 您可以将VisualBrush的可视化设置为控件,VisualBrush将重新创建控件的可视表示,而不具有任何实际功能。

I took this example from Sells/Griffiths' "Programming WPF" (Chapter 13 on Graphics) and modified it a little bit on my own, and then a bit more to demonstrate a solution for you. 我从Sells / Griffiths的“Programming WPF”(图13中的第13章)中采用了这个例子并对其进行了一些修改,然后再为您演示一个解决方案。

What this does is create a simple drawing interface (inputting the x and y coordinates of 2 points to draw a line between), but also reflects the image below. 这样做是创建一个简单的绘图界面(输入2点的x和y坐标之间绘制一条线),但也反映了下面的图像。 Its not meant to be robust at all, but it should demonstrate the functionality I think you're looking for. 它本身并不健全,但它应该展示我认为你正在寻找的功能。
The last 2 rectangle elements with the VisualBrush and SolidColorBrush are how I create the duplicated control and then shade it out. 使用VisualBrush和SolidColorBrush的最后两个矩形元素是我如何创建复制控件然后将其遮蔽。

What you can do is overlay these 2 elements over the page/window/etc that you're looking to shade out, and then make them visible when you want the effect to take place. 你可以做的是将这两个元素叠加在你想要遮挡的页面/窗口/等上,然后在你想要效果时让它们可见。

Hope this helps. 希望这可以帮助。

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

And the .cs 和.cs

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

I would attempt a dim out effect with a rectangle that fills the whole grid, is gray, has an opacity smaller than 1 and a z-index higher than your normal controls. 我会尝试使用填充整个网格的矩形进行调暗效果,灰色,不透明度小于1且z指数高于普通控件。 By default the rectangle would have visibility = collapsed, then I would use a trigger to set its visibility to visible when some appropriate "IsEnabled" property goes to "true". 默认情况下,矩形将具有visibility = collapsed,然后当某些适当的“IsEnabled”属性变为“true”时,我将使用触发器将其可见性设置为可见。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM