1

I have a Button on a MasterDetailPage and would like to send the MasterDetailPage to the Button Command as the CommandParameter, so that I can change the Detail Property of the MasterDetailPage from the ViewModel. Please let me know if I am approaching this all wrong.

MasterDetailPage

<?xml version="1.0" encoding="utf-8" ?>
<MasterDetailPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:local="clr-namespace:XamarinApp.ViewModels"
         xmlns:controls="clr-namespace:XamarinApp.Libraries.Controls"
         xmlns:telerikPrimitives="clr-namespace:Telerik.XamarinForms.Primitives;assembly=Telerik.XamarinForms.Primitives"
         x:Class="XamarinApp.Pages.MainPage"
         Icon="hamburger.png">
<MasterDetailPage.Master>
    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 Title="Master Page">
            <StackLayout>
            <Button Text="Users" Command="{Binding GoUserAccountListPage}" CommandParameter="{Binding ThisMasterDetailPage}" />
            <Button Text="Working" Command="{Binding GoWhosWorkingPage}" CommandParameter="{Binding ThisMasterDetailPage}" />
                <Button Text="Calendar" />
                <Button Text="People" />
                <Button Text="Profile"/>
            </StackLayout>
    </ContentPage>
</MasterDetailPage.Master>

ViewModel

private RelayCommand<MasterDetailPage> _goUserAccountListPage;
public RelayCommand<MasterDetailPage> GoUserAccountListPage
{
    get
    {
        return _goUserAccountListPage ?? (_goUserAccountListPage = new RelayCommand<MasterDetailPage>(masterDetail =>
        {
            masterDetail.Detail = NavigationService.GetNavigationPage(ViewModelLocator.UserAccountListPage);
            masterDetail.IsPresented = false;
        }));
    }
}
Failwyn
  • 767
  • 8
  • 22
  • 3
    This is not really good idea to access view from viewModel. I suggest to have property IsPresented in viewModel and bind it view. Or not nice way, to attach event propertyChanged in view and track viewModel.IsPresented – Julius Degutis Feb 13 '18 at 21:03
  • The Detail property of the MasterDetailPage isn’t bindable, so I guess I could just move all the logic to the MasterDetailPage CS file. Thanks. – Failwyn Feb 13 '18 at 21:46
  • You can always make your own bindable property and wrap that view page property inside, but don't know if it is worth it – Julius Degutis Feb 13 '18 at 21:48

0 Answers0