UWP - DataTemplate中的故事板ControlStoryboardAction仅在第一次触发

时间:2017-01-12 09:42:51

标签: xaml binding storyboard uwp behavior

请求是当ViewModel的属性SelectedItem引发更改时,ListView的行会闪烁。

这是我的代码,问题是它只能第一次运行。后续更改将被忽略。

<DataTemplate x:Key="myDataTemplate">
    <Grid x:Name="myGrid">
        <Interactivity:Interaction.Behaviors>
            <Core:DataTriggerBehavior Binding="{Binding SelectedItem}" Value="True">
                <Media:ControlStoryboardAction>
                    <Media:ControlStoryboardAction.Storyboard>
                        <Storyboard>
                            <ColorAnimation
                                To="#009ABF" 
                                Storyboard.TargetName="myGrid" 
                                Storyboard.TargetProperty="(Grid.Background).(SolidColorBrush.Color)" 
                                AutoReverse="True" 
                                Duration="0:0:1"
                                RepeatBehavior="1x" />
                        </Storyboard>
                    </Media:ControlStoryboardAction.Storyboard>
                </Media:ControlStoryboardAction>
            </Core:DataTriggerBehavior>
        </Interactivity:Interaction.Behaviors>
        <TextBlock Text="{Binding Name}"
            Grid.Column="1"
            VerticalAlignment="Top"
            HorizontalAlignment="Left"
            Margin="0,2,10,0"
            FontSize="16"
            TextAlignment="Left"/>

        <!--OMISSIS-->
    </Grid>

SelectedItem代码:

    public bool SelectedItem
    {
        get
        {
            return this.selectedItem;
        }
        set
        {
            this.selectedItem = value;
            this.RaisePropertyChanged();
        }
    }

1 个答案:

答案 0 :(得分:0)

这是我找到的解决方案。

1)使用故事板的已完成事件

<Storyboard Completed="SelectedItemReset" FillBehavior="Stop">

3)使用GalaSoft.MvvmLight.Messaging.Messenger从CodeBehind和ViewModel进行通信,重置属性SelectedItem

的Xaml

<ListView>
<ListView.ItemTemplate>
    <DataTemplate>
        <Grid x:Name="DataTemplateGrid">
            <Interactivity:Interaction.Behaviors>
                <Core:DataTriggerBehavior Binding="{Binding SelectedItem}" ComparisonCondition="Equal" Value="True">
                    <Media:ControlStoryboardAction ControlStoryboardOption="Play">
                        <Media:ControlStoryboardAction.Storyboard>
                            <Storyboard Completed="SelectedItemReset" FillBehavior="Stop">
                                <ColorAnimation
                                    To="Lime" 
                                    Storyboard.TargetName="DataTemplateGrid" 
                                    Storyboard.TargetProperty="(Grid.Background).(SolidColorBrush.Color)" 
                                    Duration="0:0:1"/>
                            </Storyboard>
                        </Media:ControlStoryboardAction.Storyboard>
                    </Media:ControlStoryboardAction>
                </Core:DataTriggerBehavior>
            </Interactivity:Interaction.Behaviors>

            <!--OMISSIS-->

        </Grid>
    </DataTemplate>
</ListView.ItemTemplate>

代码隐藏

private void SelectedItemReset(object sender, object e)
{
GalaSoft.MvvmLight.Messaging.Messenger.Default.Send<Mvvm.ViewModels.Units.SelectedItemResetMessage>(new Mvvm.ViewModels.Units.SelectedItemResetMessage());
}

MVVM Class .ctor

public MyViewModel()
{
    GalaSoft.MvvmLight.Messaging.Messenger.Default.Register<SelectedItemResetMessage>(this, message =>
    {
        if (this.SelectedItem == true)
            this.SelectedItem = false;
    });
}

注意:我的DataTemplate位于一个单独的文件中,并使用ItemTemplate属性链接到ListView,这使我无法调用CodeBehind中的Completed方法。