Search code examples
silverlightsilverlight-4.0uielement

How to get DataContext for UIElement?


As UIElement has no property DataContext, how can I get the DataContext for UIElement?


Solution

  • The DataContext property is introduced to the inheritance hierarchy in the FrameworkElement.

    Because FrameworkElement inherits from UIElement you have to make sure the UIElement actually is a FrameworkElement:

    if(uiElement is FrameworkElement frameworkElement)
    {
        var dc = frameworkElement.DataContext;
    }