c# - WPF Change View from a User Control MVVM -


i'm trying navigate 1 view model without side panel.

for example, have main window view, load user control.

i have tried access static instance mainviewmodel change views, it's not working.

mainwindow.xaml

<window.resources>      <datatemplate datatype="{x:type vm:firstviewmodel}">         <v:firstview/>     </datatemplate>      <datatemplate datatype="{x:type vm:secondviewmodel}">         <v:secondview/>     </datatemplate>  </window.resources> <contentcontrol content="{binding currentviewmodel}"/> 

mainviewmodel.cs

class mainviewmodel : observableobject {     private observableobject _currentviewmodel = new firstviewmodel();     public observableobject currentviewmodel     {         { return _currentviewmodel; }         set         {             _currentviewmodel = value;             raisepropertychangedevent("currentviewmodel");         }     }      private static mainviewmodel _instance = new mainviewmodel();     public static mainviewmodel instance { { return _instance; } } } 

here, have firstview, contains button , several other ui designs

firstview.xaml

<button command="{binding gotosecondview}" /> 

firstviewmodel.cs

class firstviewmodel : observableobject {     public icommand gotosecondview     {                 {             return new delegatecommand(() =>             {                 mainviewmodel.instance.currentviewmodel = new secondviewmodel();             });         }     } } 

and have secondview, similar firstview, navigates firstview

i have tried searching solution, far, have managed find examples shows buttons on panel allow switching of user control clicking button.

what trying achieve enable switching of user control via buttons on user control itself, without side panel.

any appreciated , aid me in future projects.

thank you.

you're creating 2 different instances of mainviewmodel. first 1 being created locator or , it's 1 view binding when window first created. it's creating second instance , assigning own static instance member:

private static mainviewmodel _instance = new mainviewmodel(); public static mainviewmodel instance { { return _instance; } } 

it's instance icommand handler changing when press button, isn't bound anything. speaking should using dependency injection framework ensuring it's true singleton, this:

    public mainviewmodel()     {         instance = this;     }      public static mainviewmodel instance { get; private set; } 

also code calls raisepropertychangedevent("currentviewmodel")...i'm pretty sure meant raisepropertychanged("currentviewmodel").


Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -