1

In my MainWindow.xaml I have:

<Frame Grid.Row="1" Content="{Binding MainWindowModelData.ContentView}"/>

In my MainWindowModelData I have a property called ContentView:

private Page _ContentView;
public Page ContentView
{
    get
    {
        if (_ContentView == null)
        {
            LicenceManagerModel pModel = new LicenceManagerModel();
            LicenceManagerViewModel pViewModel = new LicenceManagerViewModel(pModel);
            LicenceManagerView pView = new LicenceManagerView();
            pView.DataContext = pViewModel;

           _ContentView = pView;
        }
        return _ContentView;
        //return _ContentView ?? (_ContentView = new Page());
    }
    set
    {
        Set(ref _ContentView, value);
    }
}

The LicenceManagerView so far has the text under construction. Set(ref _ContentView, value); WILL RAISE THE PROPERTYCHANGE EVENT

When the program starts the ContentView is set and the 'under construction is shown' (It clearly has a reference to the LicenceManagerView.

In the MainWindowModel I have some code to change this ContentView property:

MenuModel pModel = new MenuModel();
MenuViewModel pViewModel = new MenuViewModel(pModel);
MenuView pView = new MenuView();
pView.DataContext = pViewModel;

ContentView = pView;

The ContentView property is raised and changed.

The problem: The ContentView is now pointing to a MenuView but the main window keeps displaying the 'Under Construction' from the LicenceManagerView.

What is going on?

grek40
  • 13,113
  • 1
  • 24
  • 50
Jos
  • 11
  • 3
  • The first thing that comes to mind is that the time between first get for LicenseManagerView and then the set to MenuView is very less. The dispatcher might be busy rendering for LicenseManager and ignoring the view change to MenuView? Is the Set to menu view right after the get? Or is there some sufficient time given between them? You could try Delay in binding like {Binding MainWindowModelData.ContentView, Delay=1000}. You can use [this](https://stackoverflow.com/questions/19654295/wpf-mvvm-navigate-views) link to switch DataTemplates instead of returning the view from ViewModel – Insane Jan 12 '18 at 04:41
  • I tried the Delay=1000 but that is not working. The main window is displaying 'under construction' when app starts and after a minute or so I will click the button which in turn changes the property ContentView. So enough time I would say. – Jos Jan 12 '18 at 05:12
  • Where do you set your DataContext? You change probably another object as set as DataContext – Rekshino Jan 12 '18 at 07:30
  • Have a look at [this SO post](https://stackoverflow.com/a/3643684/2029607) about `Frame`. However I must ask, what is _MainWindowModelData_ and what is it used for? – XAMlMAX Jan 12 '18 at 08:37
  • The data context is set through: pView.DataContext = pViewModel; – Jos Jan 14 '18 at 21:14
  • MainWindowModelData is a property of type MainWindowModel in the MainWindowViewModel. – Jos Jan 15 '18 at 00:11

1 Answers1

0

I solved the problem by implementing the following: 1: MainWindow will load a MenuView 2: MenuView has buttons on the top and main content underneath. 3. The MenuView model has a property ContentView (with propertyChange event) 4. When a different menu button is clicked the ContentView is set to a different view-viewmodel-model

NOTE: ALL ARE CODE SNIPPETS AND NOT THE WHOLE CODE FOR BREVITY.

MainWindow:

<DockPanel>
    <Frame x:Name="MainWindowFrame" NavigationUIVisibility="Hidden"/>
</DockPanel>

Code behind MainWindow:

    private void PageLoaded(object sender, RoutedEventArgs e)
    {
        MenuModel pModel = new MenuModel();
        MenuViewModel pViewModel = new MenuViewModel(pModel);
        MenuView pView = new MenuView();
        pView.DataContext = pViewModel;
        MainWindowFrame.Content = pView;
    }

MenuView has:

<Frame Content="{Binding MenuModelData.ContentView}"/>

MenuModel has:

    private Page _ContentView;
    public Page ContentView
    {
        get
        {
            if (_ContentView == null) { _ContentView = ViewLicenceManager; }
            return _ContentView;
        }
        set
        {
            Set(ref _ContentView, value);
        }
    }

MenuViewModel

    internal sealed class MenuViewModel
    {
       public MenuViewModel()
       {

       }

       public MenuViewModel(MenuModel wModel)
       {
           if (wModel == null) { return; }
           MenuModelData = wModel;
       }

       private MenuModel _MenuModelData;
       public MenuModel MenuModelData
       {
           get
           {
               if (_MenuModelData == null) { _MenuModelData = new MenuModel(); }
               return _MenuModelData;
           }
           set
           {
               MenuModelData = value;
           }
       }

   }
Jos
  • 11
  • 3