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

杀马特。学长 韩版系。学妹 提交于 2019-12-12 01:38:22

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/30922773/how-to-reset-a-wpf-controls-default-opacity-toggling-via-isenabled-after-settin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!