Search code examples
c#mvvmwinui-3winui

Why does my View still bind successfully to my ViewModel even without explicit DataContext


I'm writing an MVVM application in WinUI3, and I've run into behaviour which I don't understand. Currently my main view hosts a NavigationView control. This control contains a Frame control, who's content property is bound to a property in the ViewModel. When the user clicks on an item in the NavigationView control, the Content property of the ViewModel is set to the appropriate child ViewModel, and the Frame references a custom Data Template Selector to decide which view to instantiate as a result of this update:

RootNavigationView.xaml:

<UserControl.DataContext>
    <viewModel:RootNavigationViewModel />
</UserControl.DataContext>
<UserControl.Resources>
    <local:CustomDataTemplateSelector x:Key="templateSelector"
                                      MyViewTemplate="{StaticResource MyViewTemplate}"
                                      --and some other DataTemplates are set here as properties/>
    <DataTemplate x:Key="MyViewTemplate" x:DataType="viewModel:MyViewModel">
        <local:MyView/>
    </DataTemplate>
    // other data templates go here
</UserControl.Resources>
<NavigationView>
    <NavigationView.MenuItems>
        ...
    </NavigationView.MenuItems>
    <Frame Content="{Binding Content}" 
           ContentTemplateSelector="{StaticResource templateSelector}"/>
</NavigationView>

Weirdly, this works without me having to set the DataContext explicitly in my child views, and they all instantiate and display with zero binding errors to their respective ViewModels. My understanding was that setting the DataContext in each view told it to instantiate an appropriate ViewModel class when it was instantiated, and bind to the exposed properties of that class. Without this explicit setting, I don't understand how the View knows where to find its DataContext? I understand that DataContext is inherited by child controls, but to me this means that my child views should have their DataContext set to RootNavigationViewModel not whatever the ViewModel for that class is. I even get a design time warning about not being able to find the DataContext, and still no errors at runtime. It's actually better than I had previously, as this way I avoid my ViewModels being reinstantiated every time I switch between views, and so ViewModel data is preserved during navigation!

I would like to know what I have misunderstood about how Databinding works that my code still runs here. Is the DataTemplate somehow communicating DataContext to the view? In which case why does it behave differently to explicitly specifying the DataContext in the view?


Solution

  • The child views created by the DataTemplateSelector (CustomDataTemplateSelector) receives the Frame's Content as DataContext.