Search code examples
c#wpfeventtrigger

Issues with WPF Trigger Events


I'm trying to create some buttons in a WPF application that have trigger events based on what is currently being done to the button. I'm having trouble adding multiple trigger events. In the code below the trigger for isPressed does not work but isMouseOver does. If I remove one of the triggers it works but they never work together.

I also need to add an image change if the button is disabled but the trigger property does not seem to contain anything for disabled. I may have to make this change in the C# code. If anyone has an idea for this it would be great!

Any help is appreciated!

<Button Name="testbutton"       
        Background="Transparent"
        Cursor="Hand"
        Visibility="Visible" Grid.Column="2"  Grid.Row="1" Margin="10" ToolTip="Exits The Application">
            <Button.Template>
                <ControlTemplate TargetType="Button">
                    <StackPanel>
                        <Image Name="exitstatic"
                                Source="{StaticResource exit static}"
                                Stretch="Fill"
                                Visibility="Visible" />
                        <Image Name="exithover"
                                Source="{StaticResource exit hover}"
                                Stretch="Fill"
                                Visibility="Collapsed" />
                        <Image Name="exitdisabled"
                                Source="{StaticResource exit disabled}"
                                Stretch="Fill"
                                Visibility="Collapsed" />
                    </StackPanel>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsPressed"
                                 Value="true">
                            <Setter TargetName="exitdisabled"
                                    Property="Visibility"
                                    Value="Visible" />
                            <Setter TargetName="exithover"
                                    Property="Visibility"
                                    Value="Collapsed" />
                        </Trigger>

                        <Trigger   Property="IsMouseOver"
                                        Value="true">
                            <Setter TargetName="exithover"
                                    Property="Visibility"
                                    Value="Visible" />
                            <Setter TargetName="exitstatic"
                                    Property="Visibility"
                                    Value="Collapsed" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Button.Template>
        </Button>

Solution

  • Whenever IsPressed is true, IsMouseOver is also true, simply because the mouse has to be there to press the thing. In effect, the IsPressed trigger Setters are applied, but then the IsMouseOver trigger Setters are applied and step on what the IsPressed setters did.

    Reverse the order of the two triggers, so the IsPressed trigger will be evaluated second and step on the values set by IsPressed instead. This is a common gotcha with triggers.

    I've never had any trouble with <Trigger Property="IsEnabled" Value="False">, as far as your second question is concerned.