10

I am very new to WPF so forgive me if the question doesn't make sense. Is there an event that is fired before data context change? I want to commit the pending data changes before the data context is switched away.

H.B.
  • 166,899
  • 29
  • 327
  • 400
NS.X.
  • 2,072
  • 5
  • 28
  • 55

2 Answers2

27

There is no DataContextChanging event, but the DataContextChanged event provides the old value of the DataContext:

private void Window_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
{
    object oldDataContext = e.OldValue;
    ...
}
Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
  • 3
    The delegate is a bit different for windows runtime. The args is DataContextChangedEventArgs , and it doesn't have an oldValue property. What would be the equievalent ? – bahti Feb 23 '16 at 13:01
  • @bahti, I don't know... I'm not sure there *is* an equivalent – Thomas Levesque Feb 23 '16 at 13:28
3

There is no such event, if you want to make sure data is saved or that the user can choose to abort edits you should look into navigational architectures where screens are changed in a managed way.

H.B.
  • 166,899
  • 29
  • 327
  • 400