简体   繁体   中英

How to reset a WPF control's default opacity toggling via IsEnabled after setting opacity explictly?

When I disable a control (button), it is so dark that it is very hard to read the text.

So I am using an extension method to set the opacity to 1.0 (100%) so it can be read easily, even when disabled:

public static void IsEnabledSpecial(this System.Windows.UIElement control, bool isEnabled) {
    control.IsEnabled = isEnabled;
    control.Opacity = 1.0;          // This makes a disabled control more readable
}

Normally, when opacity is not explicitly set for a WPF control, it appears to toggle between 1.0 (100%) when the control is enabled and 0.35 (35%) when the control is disabled.

Once I explicitly set the opacity using the extension method, the control thereafter ceases to toggle between 1.0 and 0.35 when I set IsEnabled without the extension method. It gets "stuck" at 1.0 (100%), even when IsEnabled is set to false;

After I set the opacity, how can I later reset the control to do its normal opacity toggling between 1.0 and 0.35?

The changing of the Opacity is being done through triggers. By setting the value directly, you are overriding any value that may be produced by a style or triggers. This is really not the way to go about doing this sort of thing. You should be using styles and triggers of your own.

However, you may be able to achieve what you want simply by clearing the value that is assigned to Opacity :

control.ClearValue(UIElement.OpacityProperty);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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