2014-05-14 74 views
1

我遵循了这个问题描述的方法属性值保持不变:Highlighting cells in WPF DataGrid when the bound value changesWPF动画没有得到触发时

<Style x:Key="ChangedCellStyle" TargetType="DataGridCell"> 
    <Style.Triggers> 
     <EventTrigger RoutedEvent="Binding.TargetUpdated"> 
      <BeginStoryboard> 
       <Storyboard> 
        <ColorAnimation Duration="00:00:15" 
         Storyboard.TargetProperty= 
          "(DataGridCell.Background).(SolidColorBrush.Color)" 
         From="Yellow" To="Transparent" /> 
       </Storyboard> 
      </BeginStoryboard> 
     </EventTrigger> 
    </Style.Triggers> 
</Style> 

<DataGridTextColumn Header="Status" 
    Binding="{Binding Path=Status, NotifyOnTargetUpdated=True}" 
    CellStyle="{StaticResource ChangedCellStyle}" /> 

我现在面临的问题是,动画isin't得到触发,当基本属性值不会改变。在上面给出的例子中,如果“状态”属性的值没有改变,那么动画不会被触发。有没有办法,我可以触发动画,而不管值是否变化。

谢谢。

回答

1

我的猜测是,当数值没有改变时,你并没有真正在虚拟机中对属性进行更改。在MVVM中,这是非常常见的行为,在您的情况下不会引发属性更改,但在您的情况下,无论值是否更改,您都希望引发属性更改事件。

所以,如果你有这样的:

public string Status { 
    get { return _status; } 

    set { 
    if (_status == value) 
    { 
     return; 
    } 
    _status = value; 
    RaisePropertyChanged(() => Status); 
    } 
} 

将其更改为:

public string Status { 
    get { return _status; } 

    set { 
    //if (_status == value) 
    //{ 
    // return; 
    //} 
    _status = value; 

    // Following line is the key bit. When this(property-changed event) is raised, your animation should start. 
    // So whenever you need your animation to run, you need this line to execute either via this property's setter or elsewhere by directly raising it 
    RaisePropertyChanged(() => Status); 
    } 
} 

这将触发属性更改事件,每次属性的setter方法被调用,然后应触发动画不管如果值改变或没有。