显式设置不透明度后,如何通过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.
因此,我正在使用扩展方法将不透明度设置为1.0(100%),以便即使禁用也可以轻松阅读:
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
}
通常,当未为WPF控件明确设置不透明度时,启用控件时它会在1.0(100%)和禁用控件时在0.35(35%)之间切换.
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.
一旦我使用扩展方法明确设置不透明度,此后,当我设置IsEnabled 而没有扩展方法时,控件将停止在1.0和0.35之间切换.即使IsEnabled设置为false,它也会卡住"为1.0(100%);
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;
设置不透明度后,以后如何重置控件以使其正常不透明度在1.0到0.35之间切换?
After I set the opacity, how can I later reset the control to do its normal opacity toggling between 1.0 and 0.35?
推荐答案
Opacity
的更改是通过触发器进行的.通过直接设置值,您将覆盖样式或触发器可能产生的任何值.实际上,这不是执行此类操作的方法.您应该使用自己的样式和触发器.
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.
但是,您只需清除分配给Opacity
的值,就可以实现所需的目标:
However, you may be able to achieve what you want simply by clearing the value that is assigned to Opacity
:
control.ClearValue(UIElement.OpacityProperty);
这篇关于显式设置不透明度后,如何通过IsEnabled重置WPF控件的默认不透明度切换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!