Click here to Skip to main content
16,018,006 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone,
I'm new to WPF and MVVM (i use mvvm light) and I'm trying to make an application that has 2 pages. On page 1 is a button that needs to switch the current page to the second page.
When I run my application and i press the button nothing happens. No error no output. So I started debugging and everything works the property changed event gets executed and the right data is passed on.

CODE:
This is the view model for my main window. it has a frame and that frame holds the pages.
C#
using CommonServiceLocator;
using GalaSoft.MvvmLight.Views;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;

namespace WorldCraft.ViewModel
{
    public class MainWindowViewModel : MainViewModel, INotifyPropertyChanged
    {
        public MainWindowViewModel()
        {
           _page = new Pages.Home();
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        private Page page;

        public Page _page
        {
            get
            {
                return page;
            }

            set
            {
                page = value;         
                this.OnPropertyChanged("page");
                
            }
        }
    }
}


this is the xaml for the main page.
XML
<Window x:Class="WorldCraft.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:WorldCraft"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800"
        DataContext="{Binding MainWindow, Source={StaticResource Locator}}">
    <Grid>
        <Frame Content="{Binding Path=_page, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
    </Grid>
</Window>


This is the code for the first page called Home. It needs to switch to another page called WorldEditor.
C#
using CommonServiceLocator;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Ioc;
using GalaSoft.MvvmLight.Views;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using WorldCraft.ViewModel.Commands;

namespace WorldCraft.ViewModel
{
    public class HomeViewModel : MainViewModel
    {

        public NavigateCommand navigateCommand { get; private set; }

        public HomeViewModel()
        {
            navigateCommand = new NavigateCommand(SwitchTest);
            
        }

        public void SwitchTest()
        {
            ServiceLocator.Current.GetInstance<MainWindowViewModel>()._page = new Pages.WorldEditor();

        }

        


    }
}


I don't know what I'm doing wrong. Also, I don't know or this is the right way to navigate between pages. If there is a better way pls let me know.

What I have tried:

searching on the internet but it's hard to describe my problem to google if there is no output of any kind.
Posted
Updated 30-Nov-19 18:34pm
Comments
George Swan 20-Nov-19 15:53pm    
Shouldn't this.OnPropertyChanged have the parameter "Page" not "page"
dolfijn3000 20-Nov-19 15:55pm    
no, Page is the class name. page is my variable name. i need to raise the event when my variable is changed. EDIT: also propertychanged works like it needs to i checked it. its somthing with the binding that is wrong i think.
George Swan 20-Nov-19 16:02pm    
try changing it from "page" to your property name "_page"
dolfijn3000 20-Nov-19 16:07pm    
_page does not contain the value. the page data is stored in page.
George Swan 20-Nov-19 16:12pm    
the OnPropertyChanged method should reference the name of the property to which it refers which is _page not page

1 solution

this.OnPropertyChanged(nameof(_page));
should work.
notifyproperychange works on property type not of class type
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900