简体   繁体   English

动画完成后,从XAML中的情节提要中更改MVVM属性

[英]Change MVVM property from storyboard in XAML, after animation completes

I have different user controls defined as ItemSource DataTemplates for ListView, all sharing one ViewModel: 我将不同的用户控件定义为ListView的ItemSource DataTemplates,它们都共享一个ViewModel:

<UserControl>
  <Grid>
    <Style>
      <Style.Triggers>
        <DataTrigger Binding="{Binding MyAwesomeProperty}" Value="True">
          <DataTrigger.EnterActions>
            <BeginStoryboard>
              <Storyboard>
                <DoubleAnimation>
                 some awesome animation
                </DoubleAnimation>
              </Storyboard>
            </BeginStoryboard>
          </DataTrigger.EnterActions>
        </DataTrigger>
    </Style.Triggers>
</Style>
public class AwesomeViewModel : ViewModelBase
{
    private bool myAwesomeProperty= false;
    public bool MyAwesomeProperty
    {
        get { return myAwesomeProperty; }

        set
        {
            if (myAwesomeProperty!= value)
            {
                myAwesomeProperty= value;
                RaisePropertyChanged(() => MyAwesomeProperty);
            }
        }
    }
}

Now, I want to start animation when MyAwesomeProperty changes to true. 现在,我想在MyAwesomeProperty更改为true时开始动画。 This is accomplished by DataTrigger. 这是由DataTrigger完成的。 However, once animation is completed, I want to set the value of the MyAwesomeProperty back to false. 但是,一旦动画完成,我想将MyAwesomeProperty的值设置回false。

As this logic is shared by multiple UserControls having the same ViewModel, I want pure MVVM solution, without Animation.Completed callbacks in the code-behind. 由于此逻辑由具有相同ViewModel的多个UserControl共享,因此我需要纯MVVM解决方案,而在背后的代码中没有Animation.Completed回调。 Is this possible? 这可能吗?

Here's a solution using Blend's interactivity library 这是使用Blend的交互库的解决方案

First, right click References in your project, click Add Reference and under Framework add Microsoft.Expressions.Interaction and System.Windows.Interactivity . 首先,右键单击项目中的“ 引用” ,单击“ 添加引用”,然后在“ 框架”下添加Microsoft.Expressions.InteractionSystem.Windows.Interactivity

After, add the following references to the top of your XAML file: 之后,将以下引用添加到XAML文件的顶部:

xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"

Then you can do the following: 然后,您可以执行以下操作:

<Grid>
    <i:Interaction.Triggers>
        <ei:DataTrigger Binding="{Binding MyAwesomeProperty}" Value="True">
            <ei:ControlStoryboardAction Storyboard="{StaticResource YourAwesomeStoryboard}" ControlStoryboardOption="Play"/>
            <ei:ChangePropertyAction PropertyName="{Binding MyAwesomeProperty}" Value="False"/>
        </ei:DataTrigger>
    </i:Interaction.Triggers>
</Grid>

Without code in your code-behind, you simply can't. 如果没有代码,则根本无法实现。 I suggest you fire a Command on your ViewModel from code-behind to handle the logic. 我建议您从代码背后在ViewModel上触发一个Command来处理逻辑。 Like this you won't violate the MVVM pattern. 这样,您就不会违反MVVM模式。

One immediate solution I can see to this is by initiating the storyboard in code behind and subscribe to the storyboard completed event. 我可以看到的一个立即解决方案是,通过背后的代码启动情节提要并订阅情节提要完成事件。 You can start a storyboard in code like this: 您可以使用以下代码启动情节提要:

Storyboard storyBoard = (Storyboard)this.Resources["YourStoryBoard"];
storyBoard.Begin();

And then subscribe to its event 然后订阅其活动

storyboard.Completed += storyboard_Completed;

void storyboard_Completed(object sender, System.EventArgs e)
{
    // your code here
}

Hope this was of help to you 希望这对您有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM