Navigating to Page 2 on an event in Page 1 inside Frame WPF MVVM

I have a window called CreateLot.xaml which contains a frame as follows :

 <Frame Name="AddLotFrame" Content="{Binding Path=CurrentPage,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Background="Transparent"
               ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.CanContentScroll="True" ScrollViewer.VerticalScrollBarVisibility="Visible"
               MinHeight="175" MinWidth="175" ></Frame>

In the viewmodel CreateLotVm.cs :

 public CreateLotVm()
        {
            AddLotPage addLot = new AddLotPage();
            AddLotPageVm vm = new AddLotPageVm(CurrentPage);
            addLot.DataContext = vm;
            CurrentPage = addLot; 
        }

Where I am binding AddLotPage to CurrentPage of frame.

Within AddLotPage, I have a form and a button. On the button press event I am doing some validations and database transaction. Now based on data entered in AddLotPage on button press event either Page 2, Page 3 or Page 4 should be displayed. In Other words AddLotFrame should Navigate to Page 2, Page 3 or Page 4 based on data entered in AddLotPage (Page 1). So I want to update CurrentPage property of parent AddLotFrame from child AddLotPage page. How do I achieve this ?

Upvotes: 0

Views: 403

Answers (1)

mm8
mm8

Reputation: 169390

You could for example inject AddLotPage or AddLotPageVm (depending on from where you want to navigate) with a reference to CreateLotVm:

AddLotPageVm.cs:

private readonly CreateLotVm _navigationVm;
public RelayCommand ButtonCommand;
public AddLotPageVm(CreateLotVm navigationVm)
{
    _navigationVm = navigationVm;
    ButtonCommand = new RelayCommand(ButtonMethod);
}

public void ButtonMethod(object x)
{
    //...

    _navigationVm.CurrentPage = new Page2();
}

CreateLotVm.cs:

AddLotPage addLot = new AddLotPage(); 
AddLotPageVm vm = new AddLotPageVm(this);//<-- injection
addLot.DataContext = vm;
currentPage = addLot; 

Upvotes: 1

Related Questions