C# 禁用groupbox时,如何更改groupbox中的字体颜色

C# 禁用groupbox时,如何更改groupbox中的字体颜色,c#,winforms,groupbox,C#,Winforms,Groupbox,我在c#中使用了一个Groupbox,起初它是启用的 当我使用Groupbox1.Enabled=false时,它的前景色(以及其中的everythings前景色)变为默认黑色。甚至命令label1.Forecolor=Color.White也不起作用。(哪个label1在Groupbox1中)。 当我启用Groupbox时,它会修复。但是无论启用还是禁用Groupbox1,我都希望它是白色的。如果是WPF,请将其放在您的XAML资源中: <Style TargetType="Group

我在c#中使用了一个Groupbox,起初它是启用的

当我使用
Groupbox1.Enabled=false
时,它的前景色(以及其中的everythings前景色)变为默认黑色。甚至命令
label1.Forecolor=Color.White
也不起作用。(哪个
label1
Groupbox1
中)。
当我启用
Groupbox
时,它会修复。但是无论启用还是禁用
Groupbox1
,我都希望它是白色的。

如果是WPF,请将其放在您的XAML资源中:

 <Style TargetType="GroupBox" x:Key="NameOfYourStyle">
        <Style.Triggers>
            <Trigger Property="IsEnabled" Value="False">
                <Setter Property="Foreground" Value="White"/>
            </Trigger>
        </Style.Triggers>
    </Style>

将样式应用到您的GroupBox,工作就完成了

<GroupBox Style="{StaticResource NameOfYOurStyle}"/>


Dimitri

由于某些原因,无法在
WinForms
世界中设置禁用控件的前景色。取而代之的是,禁用的前颜色是从
BackColor

标签.OnPaint
(通过反射器):

但是,您可以实现如下自定义
标签
类:

public class MyLabel : Label
{
    private const ContentAlignment anyBottom = ContentAlignment.BottomRight | ContentAlignment.BottomCenter | ContentAlignment.BottomLeft;
    private const ContentAlignment anyMiddle = ContentAlignment.MiddleRight | ContentAlignment.MiddleCenter | ContentAlignment.MiddleLeft;
    private const ContentAlignment anyRight = ContentAlignment.BottomRight | ContentAlignment.MiddleRight | ContentAlignment.TopRight;
    private const ContentAlignment anyCenter = ContentAlignment.BottomCenter | ContentAlignment.MiddleCenter | ContentAlignment.TopCenter;

    protected override void OnPaint(PaintEventArgs e)
    {
        // drawing the label regularly
        if (Enabled)
        {
            base.OnPaint(e);
            return;
        }

        // drawing the background
        Rectangle backRect = new Rectangle(ClientRectangle.X - 1, ClientRectangle.Y - 1, ClientRectangle.Width + 1, ClientRectangle.Height + 1);
        if (BackColor != Color.Transparent)
        {
            using (Brush b = new SolidBrush(BackColor))
            {
                e.Graphics.FillRectangle(b, backRect);
            }
        }

        // drawing the image
        Image image = Image;
        if (image != null)
        {
            Region oldClip = e.Graphics.Clip;
            Rectangle imageBounds = CalcImageRenderBounds(image, ClientRectangle, RtlTranslateAlignment(ImageAlign));
            e.Graphics.IntersectClip(imageBounds);
            try
            {
                DrawImage(e.Graphics, image, ClientRectangle, RtlTranslateAlignment(ImageAlign));
            }
            finally
            {
                e.Graphics.Clip = oldClip;
            }
        }

        // drawing the Text
        Rectangle rect = new Rectangle(ClientRectangle.X + Padding.Left, ClientRectangle.Y + Padding.Top, ClientRectangle.Width - Padding.Horizontal, ClientRectangle.Height - Padding.Vertical);
        TextRenderer.DrawText(e.Graphics, Text, Font, rect, ForeColor, image == null ? BackColor : Color.Transparent, GetFormatFlags());
    }

    private TextFormatFlags GetFormatFlags()
    {
        TextFormatFlags flags = TextFormatFlags.GlyphOverhangPadding | TextFormatFlags.TextBoxControl | TextFormatFlags.WordBreak;

        bool isRtl = RightToLeft == RightToLeft.Yes;
        var contentAlignment = TextAlign;
        if (isRtl)
            contentAlignment = RtlTranslateContent(contentAlignment);

        if ((contentAlignment & anyBottom) != 0)
            flags |= TextFormatFlags.Bottom;
        else if ((contentAlignment & anyMiddle) != 0)
            flags |= TextFormatFlags.VerticalCenter;
        else
            flags |= TextFormatFlags.Top;

        if ((contentAlignment & anyRight) != 0)
            flags |= TextFormatFlags.Right;
        else if ((contentAlignment & anyCenter) != 0)
            flags |= TextFormatFlags.HorizontalCenter;
        else
            flags |= TextFormatFlags.Left;

        if (AutoEllipsis)
            flags |= TextFormatFlags.WordEllipsis | TextFormatFlags.EndEllipsis;
        if (isRtl)
            flags |= TextFormatFlags.RightToLeft;
        if (UseMnemonic)
            flags |= TextFormatFlags.NoPrefix;
        if (!ShowKeyboardCues)
            flags |= TextFormatFlags.HidePrefix;

        return flags;
    }
}

或者,您也不能简单地禁用GroupBox。相反,创建一个禁用其所有子级的方法

private void DisableChildren(Control control)
{
    foreach(var child in control.Controls.Cast<Control>().Where(child.GetType != typeof(Label) && child.GetType() != typeof(GroupBox)))
    {
        if(child.HasChildren)
        {
            DisableChildren(child);
        }
        child.Enabled = false;
    }
}

正如旁注:Windows窗体下的任何容器(GroupBox、Panel等)也会发生同样的情况。

我手动更改了GroupBox的前景色,这会影响文本属性,字体如下:

已使用更改所有出现的Enabled false-

            grpGeneral.ForeColor = SystemColors.GrayText;
            grpGeneral.Enabled = false;
并使用更改了所有已启用的true-

            grpGeneral.Enabled = true;
            grpGeneral.ForeColor = SystemColors.ActiveCaptionText;

WinForms?WPF?ASP.NETWebForms?ASP.NETMVC?Silverlight?我正在使用Windows FormTry在禁用GroupBox后启用label1我刚才尝试过这种方法。但它不起作用,是的。成功了。首先,我把标签放在我想要的位置,然后把Groupbox放在上面,然后把Groupbox发回去。然后它成功了。因为它不是Groupbox的一部分,也不会禁用它。非常感谢。但我想找到一种不这样做的方法。因为我把标签放在Groupbox的名称上,并用“”填充Groupbox名称。但我不喜欢只在groupbox顶部使用标签,我喜欢使用groupbox的“名称”字段。不管怎样,它成功了。谢谢,谢谢。我正在使用Windows窗体。非常感谢。我在你的第一句话里得到了答案。我以为我做错了什么。但我意识到这不是我的错,这是因为它背后的编程逻辑。这也是一个很好的解决方案。非常感谢。
            grpGeneral.ForeColor = SystemColors.GrayText;
            grpGeneral.Enabled = false;
            grpGeneral.Enabled = true;
            grpGeneral.ForeColor = SystemColors.ActiveCaptionText;