I am implementing an UserControl
and I want to detect if the DataContext is changed from the code. FrameworkElement.DataContext.Get
is not virtual so I can't override it. I can hide it with new
but I believe there is better way to do this. In WPF there is something like DataContextChanged event. Can we do something similar with Windows Phone?
In your user control constructor add this:
this.SetBinding(BoundDataContextProperty, new Binding());
Then add these:
public static readonly DependencyProperty BoundDataContextProperty = DependencyProperty.Register(
"BoundDataContext",
typeof(object),
typeof(MyUserControl),
new PropertyMetadata(null, OnBoundDataContextChanged));
private static void OnBoundDataContextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
// e.NewValue is your new DataContext
// d is your UserControl
}