0

I have problem to display another page inside MainWindow frame. When program starts it shows HomePage.xaml properly inside frame. It takes PagePath property from VM and binding works.
When I try display another Page1.xaml in MainWindowViewModel, PagePath is changing, but PropertyChange event is always null and not invoking new property to frame source.
Code looks like this:

MainWindow.xaml

<Window x:Class="TEST.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:TEST.ViewModels"
        mc:Ignorable="d"
        Title="TEST" Height="768" Width="1024"
        WindowState="Maximized">
    <Window.DataContext>
        <local:MainWindowViewModel />
    </Window.DataContext>
    <Grid>
        <Frame Source="{Binding PagePath}" NavigationUIVisibility="Hidden"  />
    </Grid>
</Window>

MainWindowViewModel.cs

namespace TEST.ViewModels
{
    public class MainWindowViewModel : INotifyPropertyChanged
    {
        private static MainWindowViewModel _instance = new MainWindowViewModel();
        public static MainWindowViewModel Instance { get { return _instance; } }

        public event PropertyChangedEventHandler PropertyChanged;

        private string pagePath = "/Pages/HomePage.xaml";

        public string PagePath
        {
            get { return pagePath; }
            set
            {
                pagePath = value;
                OnPropertyChanged(nameof(PagePath));
            }
        }

        protected void OnPropertyChanged(string propertyName = null)
        {
            if (PropertyChanged != null)
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

HomePage.xaml

<Page x:Class="TEST.Pages.HomePage"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:local="clr-namespace:TEST.ViewModels"
      mc:Ignorable="d" 
      d:DesignHeight="450" d:DesignWidth="800"
      Title="Home Page">
    <Page.DataContext>
        <local:HomePageViewModel />
    </Page.DataContext>

    <Grid>
        <StackPanel>
            <Button 
                Command="{Binding ShowPage1Command}"
                Content="Show Page1"/>
        </StackPanel>
    </Grid>
</Page>

HomePageViewModel.cs

namespace TEST.ViewModels
{
    public class HomePageViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public ICommand ShowPage1Command { get; private set; }

        public HomePageViewModel()
        {
            ShowPage1Command = new DelegateCommand((e) => ShowPage1());
        }

        protected void OnPropertyChanged(string propertyName = null)
        {
            if (PropertyChanged != null)
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        protected void ShowPage1()
        {
            MainWindowViewModel.Instance.PagePath = "/Pages/Page1.xaml";
        }
    }
}

How to solve the problem or maybe it is wrong approach to navigate with frame?

w3p
  • 55
  • 3
  • 10
  • 2
    There are two instances of MainWindowViewModel, one created by ``, the other by `_instance = new MainWindowViewModel()`. – Clemens Mar 11 '21 at 13:02
  • 1
    You may write `` – Clemens Mar 11 '21 at 13:05
  • It is working now. Thanks a lot. So now it will always use this static instance, Is it good aproach, and also to communicate between vm? – w3p Mar 11 '21 at 13:14
  • 1
    See [Why is Singleton considered an anti-pattern?](https://stackoverflow.com/questions/12755539/why-is-singleton-considered-an-anti-pattern) – Clemens Mar 11 '21 at 13:19

0 Answers0