Honestly, I don't know how to ask this one. Let me explain my scenario first.
From the answer that I have got from my last question:
...
var frame = sender as Frame;
UserControl1 uc1 = frame.Content as UserControl1;
MainViewModel mvm = uc1.DataContext as MainViewModel;
...
As you can see frame.Content
changes and it can have different DataType. Therefore, I can't just write UserControl1
. Now, what can I write as replacement for UserControl1
?
Update: Added details
frame.Content
is changing upon user interactionDataContext
of frame.Content
during runtimeAs always, please tell me if you want clarification.
If the only thing you need out of frame.Content
is a DataContext
you may try casting it to FrameworkElement
, e.g.:
var frame = sender as Frame;
FrameworkElement content = frame.Content as FrameworkElement;
MainViewModel mvm = content.DataContext as MainViewModel;
If frame.Content
is not always a FrameworkElement
you may want to check that first:
var frame = sender as Frame;
FrameworkElement content = frame.Content as FrameworkElement;
if (content != null) {
MainViewModel mvm = content.DataContext as MainViewModel;
// work with mvm...
}
else {
// Frame's content is something unexpected.
}