microsoft / XamlBehaviors

This is the official home for UWP XAML Behaviors on GitHub.
MIT License
700 stars 112 forks source link

DataTriggerBehavior doesn't evaluate initially, instead it will wait until the bound value has changed #125

Open decademoon opened 7 years ago

decademoon commented 7 years ago

Is this intended behavior?

Consider the following:

<interactivity:Interaction.Behaviors>
    <interactions:DataTriggerBehavior Binding="{x:Bind Vm.IsLiked, Mode=OneWay}" Value="False">
        <interactions:GoToStateAction StateName="NotLiked"/>
    </interactions:DataTriggerBehavior>
    <interactions:DataTriggerBehavior Binding="{x:Bind Vm.IsLiked, Mode=OneWay}" Value="True">
        <interactions:GoToStateAction StateName="Liked"/>
    </interactions:DataTriggerBehavior>
</interactivity:Interaction.Behaviors>

The visual state won't be set correctly until Vm.IsLiked actually changes value.

If this is intended behavior, then what is the best way for me to trigger an evaluation? Should I raise a property changed event for IsLiked in my view model at some point? I can't do it in the VM's constructor because there are no registered listeners yet.

MartinZikmund commented 6 years ago

I came across this as well. I would be interested in a "clean" solution too 👍

rileybwesley commented 6 years ago

@MartinZikmund, I was having the same issue. After hours of troubleshooting, it ended up being that the DataTriggerBehavior was not functioning correctly inside a ControlTemplate. I took it out of the 'ControlTemplate' and the initial binding started working.