2017-01-22 39 views
0

我有一個datagrid,其中包含一個datatrigger,當viewmodel中的值發生更改時,該datagrid會觸發行動畫。這有效,但當行進入視口時也會觸發它。所以,當我排序(通過點擊列標題),篩選或滾動,行動畫發生。不要在排序上觸發DataTrigger

如何才能讓動畫只在viewmodel中的值發生變化時觸發?

<DataGrid IsReadOnly="True" x:Name="Cards"> 
    <DataGrid.RowStyle> 
     <Style TargetType="{x:Type DataGridRow}"> 
      <Style.Triggers> 
       <DataTrigger Binding="{Binding Path=Obtained}" Value="True"> 
        <DataTrigger.EnterActions> 
         <BeginStoryboard> 
          <Storyboard> 
           <ColorAnimation Storyboard.TargetProperty="(Background).(SolidColorBrush.Color)" From="#C8E6C9" Duration="0:0:3"/> 
          </Storyboard> 
         </BeginStoryboard> 
        </DataTrigger.EnterActions> 
       </DataTrigger> 
      </Style.Triggers> 
     </Style> 
    </DataGrid.RowStyle> 
    <DataGrid.Columns> 
     <DataGridCheckBoxColumn Binding="{Binding Obtained}"/> 
     <DataGridTextColumn Header="Name" Binding="{Binding Name}"/> 
     <!-- etc... --> 
    </DataGrid.Columns> 
</DataGrid> 

我不知道這是否重要,但網格綁定到ReactiveUI IReactiveDerivedListReactiveList作爲源。

+0

背後,你試過'SuppressChangeNotifications'?看看[ReactiveUI的好東西](https://janhannemann.wordpress.com/2016/10/06/reactiveui-goodies-reactivelist/) –

+0

嗯,也許我誤解了'SuppressChangeNotifications'的作用,但我不'沒有任何問題與我的viewmodel更新我的觀點。例如,當我點擊網格中的標題來對網格進行排序時,我的問題就會出現。 – Chris

+0

你的意思是ReactiveUI處理排序? (我自己沒有用過)。如果答案是肯定的,那麼也許你應該重寫那個行爲,在你的代碼中處理排序並且不會引發任何事件。 –

回答

0

我敢肯定這不是最佳的解決方案,但希望它會指向你正確的方向。我添加了一個布爾屬性的類,我將它設置爲獲得設置爲,並在其得到方法重置爲

XAML

<DataGrid x:Name="Cards"> 
    <DataGrid.RowStyle> 
     <Style TargetType="{x:Type DataGridRow}"> 
      <Style.Triggers> 
       <DataTrigger Binding="{Binding Path=ObtainedChangedToTrue}" Value="True"> 
        <DataTrigger.EnterActions> 
         <BeginStoryboard> 
          <Storyboard> 
           <ColorAnimation Storyboard.TargetProperty="(Background).(SolidColorBrush.Color)" From="#C8E6C9" Duration="0:0:3"/> 
          </Storyboard> 
         </BeginStoryboard> 
        </DataTrigger.EnterActions> 
       </DataTrigger> 
      </Style.Triggers> 
     </Style> 
    </DataGrid.RowStyle> 
    <DataGrid.Columns> 
     <DataGridCheckBoxColumn Binding="{Binding Obtained}"/> 
     <DataGridTextColumn Header="Name" Binding="{Binding Name}"/> 
     <!-- etc... --> 
    </DataGrid.Columns> 
</DataGrid> 

代碼

private bool _obtained; 
public bool Obtained 
{ 
    get 
    { 
     return _obtained; 
    } 
    set 
    { 
     if(SetField(ref _obtained, value)) 
     { 
      ObtainedChangedToTrue = true; 
     } 
     else 
     { 
      ObtainedChangedToTrue = false; 
     } 
    } 
} 

private bool _obtainedChangedToTrue; 
public bool ObtainedChangedToTrue 
{ 
    get 
    { 
     bool result = _obtainedChangedToTrue; 
     _obtainedChangedToTrue = false; 
     return result; 
    } 
    set 
    { 
     SetField(ref _obtainedChangedToTrue, value); 
    } 
}