mans
mans

Reputation: 18168

check property when using triggers in wpf

I have a trigger in my xaml that change the text of a button, when mouse is hover over the button.

<ControlTemplate.Triggers>
    <Trigger Property="IsMouseOver" Value="True">
        <Setter Property="Foreground" Value="Cyan" />
    </Trigger>
</ControlTemplate.Triggers>

But I want that the colour doesn't changes if the button is disabled. Is there any way that I can check if control is enabled when I am changing the colour on hover on?

Edit1

Based on Bolu answer I implemented this multitrigger:

<MultiTrigger>
    <MultiTrigger.Conditions>
        <Condition Property="IsMouseOver" Value="True"/>
        <Condition Property="IsEnabled" Value="False"/>
    </MultiTrigger.Conditions>
    <MultiTrigger.Setters>
        <Setter Property="Foreground" Value="Red" />
    </MultiTrigger.Setters>
</MultiTrigger>
<MultiTrigger>
    <MultiTrigger.Conditions>
        <Condition Property="IsMouseOver" Value="True"/>
        <Condition Property="IsEnabled" Value="True"/>
 </MultiTrigger.Conditions>
 <MultiTrigger.Setters>
      <Setter Property="Foreground" Value="Green" />
  </MultiTrigger.Setters>

But it is not working properly. the triggers should change the text colour to red if the button is disable and green if it is enabled. But when the button is disabled, colour doesn't changes to Red, but when button is enabled, it changes to Green.

How can I change the xaml so the disable part works too?

Upvotes: 0

Views: 897

Answers (2)

Bolu
Bolu

Reputation: 8786

You can use a MultiTrigger to check if it is enabled.

 <ControlTemplate.Triggers>
     <MultiTrigger>
          <MultiTrigger.Conditions>
                    <Condition  Property="IsMouseOver" Value="True"/>
                    <Condition  Property="IsEnabled" Value="True"/>                                            
          </MultiTrigger.Conditions>
          <Setter Property="Foreground" Value="Cyan" />
     </MultiTrigger>
                                   
  </ControlTemplate.Triggers>

Based on your editing:

I think you could wrap your button in a panel and trigger the color using Panel's IsMouseOver plus Button's IsEnabled.

Upvotes: 1

Sheridan
Sheridan

Reputation: 69959

The change in look when the control is disabled comes from the default ControlTemplate of the Button. If you provide your own ControlTemplate, as it appears that you are doing, then that should override that change. Alternatively, you can just add a Trigger to handle the IsEnabled property to provide your own disabled look:

<ControlTemplate.Triggers>
    <Trigger Property="IsMouseOver" Value="True">
        <Setter Property="Foreground" Value="Cyan" />
    </Trigger>
    <Trigger Property="IsEnabled" Value="False">
        <!-- Put your disabled Style Setters here -->
    </Trigger>
</ControlTemplate.Triggers>

Upvotes: 1

Related Questions