7

I have a Button and its 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 enable the Button (MyBtn.IsEnabled = true) it does not change its Opacity. Why? How can I solve this problem? Thanks.

Anatoliy Nikolaev
  • 22,370
  • 15
  • 69
  • 68
Nick
  • 10,309
  • 21
  • 97
  • 201
  • At which point are you calling MyBtn.IsEnabled = true? Maybe before style is initialized? – Vale Sep 19 '12 at 11:49
  • @Vale no, i call when i click in another button, the controls have already been Loaded. – Nick Sep 19 '12 at 11:50

1 Answers1

24

A local value set on the element (Opacity="1" in your code) will always take precedence over a style or style trigger value. Please have a look at Dependency Property Setting Precedence List.

An easy fix is to set the default value on the style instead:

<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>
Julien Lebosquain
  • 40,639
  • 8
  • 105
  • 117