0

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?

Xie Steven
  • 8,544
  • 1
  • 9
  • 23
dplock
  • 11
  • 3
  • I suggested that you need to define the `EditMode` property in your model class, instead of declaring it in ViewModel's class. Then, you could directly bind to it on details page. – Xie Steven Mar 15 '19 at 07:04
  • Hi Xavier Xie - I can see how this would work. However, I think this would break the architecture of my application as my Model class is generated from the Database Schema (EntityFramework) and I would have to then 'inject' this property. – dplock Mar 15 '19 at 10:47

1 Answers1

0

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}}"
dplock
  • 11
  • 3
  • If you have resolved your issue please [mark](https://meta.stackexchange.com/a/5235) it as accepted to convenient people who visit this thread later. – Xie Steven Mar 18 '19 at 01:24