0

I have a WPF application with two pages, now I wanted to navigate to the other page when the button in first the page is clicked (I wrote the command for button in the first page), but the logic should be through the viewmodel. How to achieve this?

1
  • 1
    your question is too generic and does not belong to a problem. Search the web to get your anwser...and the command is not in the page/view... Commented Jul 31, 2017 at 11:45

2 Answers 2

1

When I write WPF applications that need to navigate to different pages, I like to follow Rachel Lim's method to implement it using DataTemplates and ViewModels. You can follow the link to her page to get the exact code for the solution, but I'll give a little summary of her method here.

In her method, she creates a ViewModel that represents the application and has a property called CurrentPage which holds a ViewModel. You can then create a command on the ApplicationViewModel called ChangePage. This command will take the ViewModel that is passed as a parameter and sets it to the CurrentPage.

The xaml takes the responsibility of switching out the correct views. When using this method, I put a ContentControl in my MainWindow and bind the Content property to ApplicationViewModel.CurrentPage. Then in the resources of the MainWindow, I create DataTemplates to tell the view "When I try to display this ViewModel, put that View on the screen".

Sign up to request clarification or add additional context in comments.

1 Comment

So essentially, use ContentControls with DataTemplates to display UserControls (Views) based on the ViewModel. No need for the bloated Frame/Page construct. It's looks way more cleaner, but you end up implementing your own navigation logic that can become tricky when demanding a navigation stack that allows pushing and poping Views. The article has some flaws, though. She is creating a ViewModel for each page and maintaining that instead of implementing such push and pop logic that would create new ViewModels on demand.
0

You don't really provide any code. But I assume your Navigation is in your code behind. You could do this by binding a Command OneWayToSource.

XAML

<local:MainWindow x:Class="WpfNameSpace.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfNameSpace"
        xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
        mc:Ignorable="d"
        NavigatePageCommand="{Binding Path=MyViewModel.NavigateCommand, Mode=OneWayToSource}"
        Title="MainWindow" Height="600" Width="800">
    <Grid>

    </Grid>
</local:MainWindow>

Please take a look at local:MainWindow.

C#

public partial class MainWindow : Window
{
    public ICommand NavigatePageCommand
    {
        get { return (ICommand) GetValue(NavigatePageCommandProperty); }
        set { SetValue(NavigatePageCommandProperty, value); }
    }

    // Using a DependencyProperty as the backing store for NavigatePageCommand.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty NavigatePageCommandProperty =
        DependencyProperty.Register("NavigatePageCommand", typeof(ICommand), typeof(MainWindow),
            new PropertyMetadata(0));

    public MainWindow()
    {
        InitializeComponent();
        NavigatePageCommand = new RelayCommand(Navigate);

    }

    public void Navigate()
    {
        //Do Navigation here
    }

}

I assume you are familiar with Commands, ViewModels and Bindings and you get the idea.

1 Comment

Thanks for response @Peter, i will try this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.