I have a usercontrol (UC) that is sometimes in a window and sometimes in a tab and sometimes in a childwindow. When it’s in a window I pass the window the viewmodel. In the UC I’d like to check if a datacontext already exists in an ancestor of the UC (not specifically the window because it has no knowledge if it’s inside a window or a tab) so that I can instantiate a viewmodel is it’s not. How can I check for this?
Share
If a DataContext is set for a visual ancestor, the UC inherits it, so you can just check if the DataContext of the UC is null.