WPF Change View from MVVM Custom Control

I'm trying to go from one View Model

to the other without any sidebar.

For example, I have Main Window View

, this is where I download User Control

.

I tried to access the static instance from MainViewModel

in order to change the views, but it doesn't work.

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
    {
        get { return _currentViewModel; }
        set
        {
            _currentViewModel = value;
            RaisePropertyChangedEvent("CurrentViewModel");
        }
    }

    private static MainViewModel _instance = new MainViewModel();
    public static MainViewModel Instance { get { return _instance; } }
}

      

Here I have it FirstView

, it just contains a button and a few other UIs

FirstView.xaml

<Button Command="{Binding goToSecondView}" />

      

FirstViewModel.cs

class FirstViewModel : ObservableObject
{
    public ICommand goToSecondView
    {
        get
        {
            return new DelegateCommand(() =>
            {
                MainViewModel.Instance.CurrentViewModel = new SecondViewModel();
            });
        }
    }
}

      

And I have SecondView

one that looks like FirstView

, it just goes toFirstView

I tried to find a solution, but so far I have been able to find examples showing buttons on a panel that then allow you to toggle User Control

to clicking that button.

I am trying to achieve to enable User Control

with buttons itself User Control

without any sidebar.

Any help would be much appreciated and will definitely help me on my future projects.

Thank.

+3


source to share


2 answers


You are creating two different instances of MainViewModel. The first one is probably created by a locator or something, and the one with which your view binds to when the window is first created. It then creates a second instance and assigns it to its own static instance member:

private static MainViewModel _instance = new MainViewModel();
public static MainViewModel Instance { get { return _instance; } }

      

In this case, your ICommand handler changes when you click on a button that is not attached to anything. Generally speaking, you should be using a Dependency Injection framework to provide its true singleton, but for now just do something like this:



    public MainViewModel()
    {
        Instance = this;
    }

    public static MainViewModel Instance { get; private set; }

      

Also your code calls RaisePropertyChangedEvent ("CurrentViewModel") ... I'm sure what you meant is RaisePropertyChanged ("CurrentViewModel").

+2


source


i would go the other way and expose the Event from your first and second view models like

public event EventHandler<ShowMeEventArgs> ShowMeEvent;

      



then you just need to subscribe to this event and you can show the viewmodel. and even more your first and second viewmodel should not know anything from the mainviewmodel

0


source







All Articles