Question

I have followed the approach described in this question : Highlighting cells in WPF DataGrid when the bound value changes

<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}" />

The issue I am facing is that the animation isin't getting triggered, when the underlying property value doesn't change. In the example given above, if the value of "Status" property doesn't change, then the animation doesn't get triggered. Is there a way, I can trigger the animation irrespective of whether the value changes or not.

Thanks.

Was it helpful?

Solution

My guess is you're not actually doing the change in value in your VM for the property when the value does not change. This is pretty common behavior in MVVM to not raise property-changed when it aint really needed, in your case however you would want to raise the property changed event regardless of the value changing or not.

So if you have something like:

public string Status {
  get { return _status; }

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

change it to:

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);
  }
}

This will fire the property changed event everytime the property's setter is invoked which should then trigger your animation regardless of if the value changed or not.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top