Detecting a Change of Data Context

The FrameworkElement.DataContext property is a fundamental part of the XAML data-binding system and allows an object to be associated with a top-level element in the visual tree and then inherited by descendants of that element.

Unfortunately, the Windows Phone SDK does not include a public event for detecting when the DataContext of a FrameworkElement is changed. As a workaround, I have included in the downloadable sample code a class named DataContextChangedListener that uses attached properties to emulate a DataContextChanged event, which allows you to receive notification when a FrameworkElement’s data context is changed (see Listing 26.4).

The Subscribe method of the DataContextChangedListener associates ...

Get Windows® Phone 8 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.