简体   繁体   English

WPF从用户控件MVVM更改视图

[英]WPF Change View from a User Control MVVM

I'm trying to navigate from one View Model to another without any side panel. 我试图从一个View Model导航到另一个没有任何侧面板。

For example, I have a Main Window View , this is where I load my User Control . 例如,我有一个Main Window View ,这是我加载User Control

I have tried to access the static instance from the MainViewModel to change the Views, but it's not working. 我试图从MainViewModel访问静态实例来更改视图,但它不起作用。

MainWindow.xaml 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 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 my FirstView , it just contains a button and several other UI designs 在这里,我有我的FirstView ,它只包含一个按钮和其他几个UI设计

FirstView.xaml FirstView.xaml

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

FirstViewModel.cs FirstViewModel.cs

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

And I have a SecondView , which is similar to FirstView , just that it navigates to FirstView 我有一个SecondView ,它类似于FirstView ,只是导航到FirstView

I have tried searching for a solution, but so far, I have only managed to find examples that shows buttons on a panel which then allow switching of the User Control from clicking those button. 我已经尝试过寻找解决方案,但到目前为止,我只是设法找到一个显示面板上按钮的示例,然后允许切换User Control不是单击这些按钮。

What I am trying to achieve is to enable switching of User Control via the buttons on the User Control itself, without any side panel. 我想要实现的是通过User Control本身的按钮启用User Control切换,无需任何侧面板。

Any help would be very much appreciated and would definitely aid me in my future projects. 任何帮助将非常感谢,肯定会帮助我在未来的项目中。

Thank You. 谢谢。

You're creating two different instances of MainViewModel. 您正在创建两个不同的MainViewModel实例。 The first one is probably being created by a locator or something and it's the one that your view is binding to when the window is first created. 第一个可能是由定位器或其他东西创建的,它是您的视图在首次创建窗口时绑定到的那个。 It's then creating a second instance and assigning it to its own static Instance member: 然后创建第二个实例并将其分配给自己的静态实例成员:

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

It's this instance that your ICommand handler is changing when you press the button, which isn't bound to anything. 正是这个实例,当你按下按钮时,你的ICommand处理程序正在改变,而按钮没有任何约束。 Generally speaking you should be using a dependency injection framework to ensuring it's a 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 pretty sure you meant that to be RaisePropertyChanged("CurrentViewModel"). 你的代码也调用了RaisePropertyChangedEvent(“CurrentViewModel”)......我很确定你的意思是RaisePropertyChanged(“CurrentViewModel”)。

i would go another way and expose an Event from your first and Second viewmodel like 我会采取另一种方式,从你的第一个和第二个视图模型中公开一个事件

public event EventHandler<ShowMeEventArgs> ShowMeEvent;

then your main just need to subscribe to this event and can show the viewmodel. 然后你的主要只需要订阅这个事件,并可以显示viewmodel。 and even more your first and second viewmodel dont need to know anything from mainviewmodel 甚至更多你的第一个和第二个视图模型不需要知道mainviewmodel中的任何内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM