microsoft / XamlBehaviorsWpf

Home for WPF XAML Behaviors on GitHub.
MIT License
854 stars 140 forks source link

Reversibility of property changes for triggers #106

Closed user98392 closed 2 years ago

user98392 commented 2 years ago

The problem is that we burdened to duplicate our triggers in order to restore state of properties. For example, data triggers from WPF can do that.

brianlagunas commented 2 years ago

There is not enough information in your issue to understand the problem you are having. Please provide more detail and a sample app to reproduce the issue.

user98392 commented 2 years ago

So for example:

https://github.com/user98392/Example/blob/91e0ae747756109eb50c91e5a8f2c46246d2d808/MainWindow.xaml#L52-L60

Just for changing one property I need to have a pair of triggers where the second one is reverting back changes. Is it possible to implement such reversibility for triggers?

brianlagunas commented 2 years ago

Generally speaking, a DataTrigger will put properties back to their previous value when the specified condition is no longer true.

However, the same is not true for actions. There's no way for the trigger to know what action would "undo" the effect of the triggered action, assuming that there is even such an action.

For what you are trying to do, I would recommend using a Style Trigger instead. Remove the Button.Effect from your Button in MainWindow.xaml and update your ButtonStyle to use a trigger instead.

Something like this:

<Setter Property="Button.Effect">
    <Setter.Value>
        <DropShadowEffect ShadowDepth="0" Color="#36096D" />
    </Setter.Value>
</Setter>

<Style.Triggers>
    <Trigger Property="IsMouseOver" Value="True">
        <Trigger.EnterActions>
            <BeginStoryboard>
                <Storyboard>
                    <DoubleAnimation Storyboard.TargetProperty="(Button.Effect).BlurRadius" To="20" Duration="0:0:0.2"/>
                </Storyboard>
            </BeginStoryboard>
        </Trigger.EnterActions>
        <Trigger.ExitActions>
            <BeginStoryboard>
                <Storyboard>
                    <DoubleAnimation Storyboard.TargetProperty="(Button.Effect).BlurRadius" To="0" Duration="0:0:0.2"/>
                </Storyboard>
            </BeginStoryboard>
        </Trigger.ExitActions>
    </Trigger>
</Style.Triggers>