How to animate TextBlock when its value changes in WinRT XAML?

自作多情 提交于 2019-12-05 21:43:20

Again, not sure if we are 100% agreeing. But, still, here's how you can do it:

public class MyViewModel : INotifyPropertyChanged
{
    public string Text { get; set; }
    public event PropertyChangedEventHandler PropertyChanged;
}

public void Loaded()
{
    var myBox = new TextBox();
    var myAni = new Storyboard();
    var MyVvm = new MyViewModel();

    // sensible approach
    myBox.TextChanged += (s, e) => myAni.Begin();

    // forced approach
    MyVvm.PropertyChanged += (s, e) =>
    {
        if (e.PropertyName.Equals("Text"))
            myAni.Begin();
    };
}

In the end, you are the developer of your own app. not me.

If you are willing to use behaviors, you can also do the same thing this way:

<Page.Resources>
    <Storyboard x:Name="FadeAway">
        <DoubleAnimation Duration="0:0:1" To="0" Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="textBox" d:IsOptimized="True"/>
    </Storyboard>
</Page.Resources>

<TextBox x:Name="textBox">
    <Interactivity:Interaction.Behaviors>
        <Core:EventTriggerBehavior EventName="TextChanged">
            <Media:ControlStoryboardAction Storyboard="{StaticResource FadeAway}"/>
        </Core:EventTriggerBehavior>
    </Interactivity:Interaction.Behaviors>
</TextBox>

I suppose you can have your "pure" MVVM approach using a behavior. It gets you 100% XAML, and that makes some developers feel warm and fuzzy; I get that. And, I like behaviors. Look. I don't want to argue with you here, it's just that the top approach is certainly not "wrong".

Learn more about behaviors: http://blog.jerrynixon.com/2013/10/everything-i-know-about-behaviors-in.html

Best of luck.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!