简体   繁体   English

wpf禁用不透明度级别

[英]wpf disabled opacity level

I need to change the opacity of controls within a container (grid/dockpanel/etc) when they are disabled, so they appear brighter, or less dimmed. 禁用它们时,我需要更改容器(网格/扩展面板/等)中控件的不透明度,以使它们看起来更亮或更暗。

When the container is disabled, nested controls appear to be reduced to about 0.5 (say) opacity. 禁用容器后,嵌套控件似乎减少到大约0.5(例如)不透明度。 If I try to adjust the opacity of the container when disabled, the max is always limited to 0.5, no matter what I set it to. 如果禁用后我尝试调整容器的不透明度,则无论将其设置为什么,最大值始终限制为0.5。 The method of assigning the opacity doesn't matter either, I have tried directly in code and with styles/triggers. 分配不透明度的方法也无关紧要,我已经在代码中直接尝试了样式/触发。

I am guessing this is implemented in the container by masking with a rectangle (or other window type) and setting the opacity. 我猜想这是通过用矩形(或其他窗口类型)遮罩并设置不透明度来在容器中实现的。 Then, from MSDN it says : 然后, 从MSDN说

Opacity is applied from parent elements on down the element tree to child elements, but the visible effects of the nested opacity settings aren't indicated in the property value of individual child elements. 不透明度是从元素树上的父元素应用于子元素,但是嵌套的不透明度设置的可见效果未在单个子元素的属性值中指示。 For instance, if a list has a 50% (0.5) opacity and one of its list items has its own opacity set to 20% (0.2), the net visible opacity for that list item will be rendered as if it were 10% (0.1), but the property value of the list item Opacity property would still be 0.2 when queried. 例如,如果列表的不透明度为50%(0.5),并且其中一个列表项的不透明度设置为20%(0.2),则该列表项的净可见不透明度将呈现为10%( 0.1),但查询时列表项“不透明度”属性的属性值仍为0.2。

So I understand why I am seeing this behaviour, I am just hoping there is a way to override it? 所以我明白为什么我会看到这种行为,我只是希望有一种方法可以替代它?

If not, the only alternative I can think of is to roll my own disabled behaviour with my own rectangle, and I can then set the opacity level I need. 如果没有,我唯一想到的替代方法是使用自己的矩形滚动禁用的行为,然后可以设置所需的不透明度级别。 Just sounds like unnecessary hassle to me, unless I am missing something? 听起来好像对我来说是不必要的麻烦,除非我想念什么?

Note that I was interested in this question , but setting the opacity from code in this manner doesn't do anything different, the disabled controls appear unaffected. 请注意,我对此问题感兴趣 ,但是以这种方式设置代码的不透明度并没有什么不同,禁用的控件似乎不受影响。

Normally disabled state in WPF controls is rendered by using different Background and Foreground brushes which are set using a trigger in the control's template rather than changing opacity. WPF控件中通常禁用的状态是通过使用不同的“背景”和“前景”笔刷渲染的,这些笔刷是通过控件模板中的触发器设置的,而不是更改不透明度。 Eg for a TextBox it has this trigger: 例如,对于TextBox,它具有以下触发器:

<ControlTemplate.Triggers>
    <Trigger Property="IsEnabled" Value="false">
        <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
    </Trigger>
</ControlTemplate.Triggers>

The opacity is unchanged. 不透明度不变。 If you have a GroupBox with IsEnabled="False" then all child controls will inherit the IsEnabled state. 如果您有一个具有IsEnabled =“ False”的GroupBox,则所有子控件都将继承IsEnabled状态。

If you want to change the disabled appearance in some way then create your own template style or modify the default one. 如果要以某种方式更改禁用的外观,请创建自己的模板样式或修改默认样式。

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

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