WPF MVVM 切换用户控件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19501266/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:54:44  来源:igfitidea点击:

WPF MVVM switch usercontrols

c#wpfmvvmuser-controls

提问by Chris

I am new to MVVM and WPF but I know what's going on in MVVM. I have a problem with switching between user controls in mainwindow. In my app I have: MainWindow.xaml with log and 2 links: Show all and Create new. Of course I have ViewModel for it. I have 2 more UserControls: ShowAll and Create with ViewModels and all logic in it (adding data etc). How can I show create form when I click link Create new or show all when I click ShowAll?

我是 MVVM 和 WPF 的新手,但我知道 MVVM 中发生了什么。我在主窗口中的用户控件之间切换时遇到问题。在我的应用程序中,我有: MainWindow.xaml with log 和 2 个链接:Show all 和 Create new。当然,我有它的 ViewModel。我还有 2 个用户控件:ShowAll 和 Create with ViewModels 以及其中的所有逻辑(添加数据等)。单击“新建”链接时如何显示创建表单或单击“全部显示”时显示全部?

In windowForms I just hide UC, buto here is no code behind :)

在windowForms中,我只是隐藏了UC,但这里没有代码:)

My MainWindow.xaml:

我的 MainWindow.xaml:

<Window x:Class="Test.Views.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="300" Width="300">
    <Grid>
        <StackPanel>
            <TextBox Text="{Binding Name}"/>
            <Button Content="Change" Command="{Binding ChangeCommand}"/>
        </StackPanel>
    </Grid>
</Window>

My MainWindowViewModel:

我的主窗口视图模型:

class MainWindowViewModel : BaseViewModel
{
    private Person _person;
    private BaseCommand _changeCommand;

    public MainWindowViewModel()
    {
        _person = new Person();
    }

    public string Name
    {
        get
        {
            return _person.Name;
        }
        set
        {
            if (_person.Name != value)
                _person.Name = value;
            OnPropertyChanged(() => Name);
        }
    }

    public ICommand ChangeCommand
    {
        get
        {
            if (_changeCommand == null)
                _changeCommand = new BaseCommand(() => change());
            return _changeCommand;
        }
    }

    private void change()
    {
        _person = new Person();
        Name = _person.Imie;
    }
}

In Create and ShowAll there is no code. In xaml only a label, VM is empty. Just for test.

在 Create 和 ShowAll 中没有代码。在xaml中只有一个标签,VM是空的。只是为了测试。

Thank's for help!

感谢帮助!

回答by Slugart

You can use a ContentControlto display a specific DataTemplatebased on the type of ViewModel that is bound to the ContentControl.

您可以使用ContentControl来显示DataTemplate基于绑定到ContentControl.

http://www.japf.fr/2009/03/thinking-with-mvvm-data-templates-contentcontrol/

http://www.japf.fr/2009/03/thinking-with-mvvm-data-templates-contentcontrol/

The command that is bound to the ShowAll button can simply change a property on your main ViewModel which is what is bound to your content control.

绑定到 ShowAll 按钮的命令可以简单地更改主 ViewModel 上的一个属性,该属性绑定到您的内容控件。