I have an existing UWP app to manage passwords for websites and accounts but when I wrote it about 3 years ago I did not know MVVM very well and so all the event handlers are in the View's code behind and now I'm trying to resolve this and make it more MVVM adherent by moving this code to the ViewModel.
One of the existing features I have is a flyout menu on each ListView item so the user can edit/delete the entry (and do a couple of other functions) but because I am defining a DataType for the ListView ItemTemplate data template it will now not recognise the binding to the Click event handler in my Viewmodel.
As you'd expect I have my ViewModel defined both in the namespaces and in the page data context as follows:
<Page ....
xmlns:vm="using:PassPort.ViewModels"
...>
<Page.DataContext>
<vm:MainViewModel/>
</Page.DataContext>
And here is my ListView and it's ItemTemplate and DataTemplate. In each MenuFlyoutItem in the FlyOut I'm trying to tell it to use the handler in my ViewModel but it cannot resolve 'vm' - it shows the squiggly line and says "the property 'vm' was not found in type 'Account'".
</ListView Name="lvwAccounts"
ItemsSource="{x:Bind vm.AccountsView}"
.....>
<ListView.ItemTemplate>
<DataTemplate x:DataType="model:Account">
<Grid MinHeight="36" HorizontalAlignment="Left" RightTapped="AccountsList_RightTapped">
<FlyoutBase.AttachedFlyout>
<MenuFlyout Placement="Bottom">
<MenuFlyoutItem x:Name="OpenWebsiteButton" Text="Open Website" Click="{x:Bind vm.FlyoutOpenWebsiteButton_Click}"/>
<MenuFlyoutItem x:Name="EditButton" Text="Edit Account" Click="{x:Bind vm.FlyoutEditButton_Click}"/>
<MenuFlyoutItem x:Name="AddButton" Text="Add Account" Click="{x:Bind vm. FlyoutAddButton_Click}"/>
<MenuFlyoutItem x:Name="DeleteButton" Text="Delete Account" Click="{x:Bind vm.FlyoutDeleteButton_Click}"/>
</MenuFlyout>
</FlyoutBase.AttachedFlyout>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200"/>
<ColumnDefinition Width="150"/>
<ColumnDefinition Width="200"/>
<ColumnDefinition Width="150"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" Text="{x:Bind AccountName}" Foreground="{StaticResource Light}"
VerticalAlignment="Center" HorizontalAlignment="Stretch"/>
<TextBlock Grid.Row="0" Grid.Column="1" Text="{x:Bind Category}" Foreground="{StaticResource Light}"
VerticalAlignment="Center" HorizontalAlignment="Stretch" TextWrapping="Wrap" />
<TextBlock Grid.Row="0" Grid.Column="2" Text="{x:Bind UserID}" Foreground="{StaticResource Light}"
VerticalAlignment="Center" HorizontalAlignment="Left"/>
<TextBlock Grid.Row="0" Grid.Column="3" Text="{x:Bind Password}" Foreground="{StaticResource Light}"
VerticalAlignment="Center" HorizontalAlignment="Left" />
<TextBlock Grid.Row="0" Grid.Column="4" Text="{x:Bind PasswordHint}" Foreground="{StaticResource Light}"/>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Everywhere else in the code 'vm' is resolved without issue so it seems that because of the DataType it then can't/won't resolve my ViewModel reference.
I've also tried using 'Click="{Binding vm.[event handler]"} but it makes no difference - so does anyone know how I can resolve this?