As UIElement
has no property DataContext
, how can I get the DataContext
for UIElement
?
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;
}