2014-05-14 75 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方法被調用,然後應觸發動畫不管如果值改變或沒有。