2017-01-12 68 views
1

請求的是當ViewModel的屬性SelectedItem引發更改時,我的ListView的行閃爍。UWP - DataTemplate中的故事板ControlStoryboardAction僅第一次觸發

這是我的代碼,問題是它只能第一次運行。後續更改將被忽略。

<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(); 
     } 
    } 
+0

什麼是'SelectedItem'類型?,請提供cs代碼 –

+0

乍一看,它只是您的'RepeatBehavior =「1x」',因爲它對每個實例使用相同的Storyboard,並且您已對其設置了'AutoReverse'。 –

回答

0

這是我找到了解決辦法。

1)使用的故事板

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

3完成事件)使用GalaSoft.MvvmLight.Messaging.Messenger從代碼隱藏和視圖模型到comunicate屬性的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> 

CodeBehind

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控件,這讓我無法調用代碼隱藏已完成的方法。