IsEnabled Trigger button not working

I have Buttonhis style:

<Button Name="MyBtn" Style="{StaticResource ButtonEnabledStyle}"
        IsEnabled="False" Opacity="1" />

<Style x:Key="ButtonEnabledStyle" TargetType="Button">
    <Style.Triggers>
        <Trigger Property="IsEnabled" Value="True" >
            <Setter Property="Opacity" Value="0.1" />
        </Trigger>
    </Style.Triggers>
</Style>

But when I turn on the button ( MyBtn.IsEnabled = true), it does not change its opacity. What for? How can I solve this problem? Thank.

+5
source share
1 answer

The local value set in the element ( Opacity="1"in your code) will always take precedence over the value of the style or style. Please see the Priority List of Dependency Properties .

An easy fix is ​​to set a default for the style:

<Style x:Key="ButtonEnabledStyle" TargetType="Button">
  <Setter Property="Opacity" Value="1.0" />
  <Style.Triggers>
    <Trigger Property="IsEnabled" Value="True" >
      <Setter Property="Opacity" Value="0.1" />
    </Trigger>
  </Style.Triggers>
</Style>
+15
source

All Articles