【问题标题】:UWP - Storyboard in DataTemplate ControlStoryboardAction fires only first timeUWP - DataTemplate ControlStoryboardAction 中的情节提要仅第一次触发
【发布时间】:2017-05-27 08:33:51
【问题描述】:

请求是当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>

所选项目代码:

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

【问题讨论】:

  • SelectedItem是什么类型?请提供cs代码
  • 乍一看,这只是您的RepeatBehavior="1x",因为它为每个实例使用相同的故事板,并且您在上面设置了AutoReverse

标签: xaml binding storyboard uwp behavior


【解决方案1】:

这是我找到的解决方案。

1) 使用 Storyboard 的 Completed 事件

<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 类.ctor

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

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

【讨论】:

    猜你喜欢
    • 2019-04-25
    • 1970-01-01
    • 2023-03-21
    • 2016-05-02
    • 1970-01-01
    • 2018-09-27
    • 1970-01-01
    • 2012-04-27
    • 2020-10-08
    相关资源
    最近更新 更多