wpftriggersstylesconditional-statementsmultitrigger

MultiTrigger executes only once


My button is supposed to change colour after certain amount of mouse overs, however Multibinding doesn't work properly.

Buttons in my application has the following style:

    <Style TargetType="Button">
        <EventSetter Event="MouseEnter" Handler="OnMouseEnterButton"/>
        <Style.Triggers>
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="Content" Value="0"/>
                    <Condition Property="IsMouseOver" Value="False"/>
                </MultiTrigger.Conditions>

                <MultiTrigger.Setters>
                    <Setter Property="Background" Value="Green"/>
                </MultiTrigger.Setters>
            </MultiTrigger>
        </Style.Triggers>
   </Style>

My Button looks like this:

<Button Name="button1">1</Button>

With the following event handler:

private void OnMouseEnterButton(object sender, RoutedEventArgs e)
{
    ((Button)sender).Content = (int.Parse(((Button)sender).Content.ToString())) + 1;
}

However if the value of the Button.Content condition is different from initial value. For example: <Condition Property="Content" Value="10"/> Triggers stop working.


Solution

  • The problem is you are comparing a System.Int32 value (set in code) with a System.String value (defined in condition).

    There are two ways to fix this:

    1) Change style condition to:

        <Condition Property="Content">
            <Condition.Value>
                <sys:Int32>10</sys:Int32>
            </Condition.Value>
        </Condition>
    

    where you have to add a namespace xmlns:sys="clr-namespace:System;assembly=mscorlib"

    or change you code to :

    ((Button)sender).Content = ((int.Parse(((Button)sender).Content.ToString())) + 1).ToString();