Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF: Remove control's explicit foreground color

Two sample TextBoxes in a standard color scheme and the following constructor yield Box1 with a gray foreground and Box2 with a black foreground, since Box2's foreground color has been explicitly set.

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        Box2.Foreground = Brushes.Black;
        Box1.IsEnabled = false;
        Box2.IsEnabled = false;
    }
}

I would like to "unset" the foreground color so Box2 "falls back" to the default disabled color and has a gray foreground when IsEnabled is set to false. Is this possible? If so, how is it done?

Setting the Foreground property to null does not have the desired effect. I want to avoid explicitly setting the Foreground color to Gray if possible, since it would not compatible with customized color schemes.

like image 719
Caleb Avatar asked Mar 02 '26 11:03

Caleb


1 Answers

I am not sure if that's what you mean, but try following code:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        Box2.Foreground = Brushes.Black;
        Box1.IsEnabled = false;
        Box2.IsEnabled = false;
        Box2.ClearValue(TextBox.ForegroundProperty);
    }
}
like image 66
lipcu Avatar answered Mar 05 '26 01:03

lipcu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!