Search code examples
mvvm-lightuwp-xamlwindows-template-studio

Windows Template Studio MVVMLight accessing ViewModel from Details page


I'm using Windows Template Studio V3.0 the create a UWP App using MVVMLight that implements a Master/Detail page. I'm sure I'm going to kick myself but I can't work out how to access the ViewModel from the Details page. I need to access a property on the View Model to set the Visibility of some XAML elements.

In previous versions of the WTS I could use the Locator to access the ViewModel as follows:

<TextBlock
    Margin="8"
    Text="{x:Bind MasterMenuItem.Name , Mode=OneWay}"
    Style="{StaticResource BodyTextBlockStyle}"
    Visibility="{Binding Source={StaticResource Locator}, Path=DriverDetailsViewModel.EditMode, Converter={StaticResource InverseBoolToVisibilityConverter}}" />

In V3 of WTS the implementation of the Locator has changed and does not appear to accessible as a Static Resource?


Solution

  • As I suspected, after studying the WTS code a bit more I realised that I can access the ViewModelLocator from the XAML code-behind. I just needed to add the following to my detailsview code-behind:

    private DriverDetailsViewModel ViewModel
    {
        get { return ViewModelLocator.Current.DriverDetailsViewModel; }
    }
    

    Then the following XAML works

    Visibility="{x:Bind ViewModel.IsEditMode, Mode=OneWay, Converter={StaticResource InverseBoolToVisibilityConverter}}"