Caleb
Caleb

Reputation: 299

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.

Upvotes: 3

Views: 2524

Answers (2)

lipcu
lipcu

Reputation: 106

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

Upvotes: 8

renanleandrof
renanleandrof

Reputation: 7007

Use the event IsEnabledChanged to set the foreground of the box.

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

        Box2.Foreground = Brushes.Black;
        Box1.IsEnabled = false;
        Box2.IsEnabled = false;
        Box1.IsEnabledChanged += new DependencyPropertyChangedEventHandler(label1_IsEnabledChanged);
    }

    void label1_IsEnabledChanged( object sender, DependencyPropertyChangedEventArgs e ) {
        //Set the foreground you want here!
    }

}

But if you don't want to explicit set the color, try to set it to Transparent o.O

Upvotes: 0

Related Questions