简体   繁体   English

显式设置不透明度后,如何通过IsEnabled重置WPF控件的默认不透明度切换?

[英]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: 因此,我正在使用扩展方法将不透明度设置为1.0(100%),以便即使禁用也可以轻松阅读:

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. 通常,当未为WPF控件显式设置不透明度时,启用控件时它会在1.0(100%)和禁用控件时在0.35(35%)之间切换。

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. 一旦我使用扩展方法明确设置了不透明度,此后,当我在使用扩展方法的情况下设置IsEnabled时,控件将停止在1.0和0.35之间切换。 It gets "stuck" at 1.0 (100%), even when IsEnabled is set to false; 即使IsEnabled设置为false,它也会“卡住”为1.0(100%)。

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.0和0.35之间切换?

The changing of the Opacity is being done through triggers. Opacity的更改是通过触发器完成的。 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 : 但是,您只需清除分配给Opacity的值,便可以实现所需的功能:

control.ClearValue(UIElement.OpacityProperty);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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