WPF:使用 MVVM 模式执行 Tabcontrol 的正确方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17875170/
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:17:22  来源:igfitidea点击:

WPF: right way to do a Tabcontrol with MVVM pattern

wpfmvvmviewmodeltabcontrol

提问by Oscar Mateu

First of all, I'm newbie in WPF and specially in MVVM. I have a window with diferent tabs and a very large ViewModel with the business logic of the content of every tab. I know it is not right, so now I'm trying to do it more elegant:

首先,我是 WPF 的新手,特别是 MVVM。我有一个带有不同选项卡的窗口和一个非常大的 ViewModel,其中包含每个选项卡内容的业务逻辑。我知道这是不对的,所以现在我试图做得更优雅:

As I see googling, an idea is to do a collection of a "base" viewmodel from wich inherit the sub-viewmodels of every tab, and a collection on this "base" viewmodel in the viewmodel of the window.

正如我看到的谷歌搜索,一个想法是做一个“基本”视图模型的集合,从它继承每个选项卡的子视图模型,以及窗口视图模型中这个“基本”视图模型的集合。

TabBaseViewModel
Tab1ViewModel inherits TabBaseViewModel
Tab2ViewModel inherits TabBaseViewModel

MainWindow ViewModel--> Collectionof TabBaseViewModel

MainWindow ViewModel-->CollectionTabBaseViewModel

The contents the tabs do not have anything in common along each other.

选项卡的内容彼此没有任何共同点。

How I have to proceed?

我必须如何进行?

采纳答案by devdigital

You should consider using an MVVM framework if you're using MVVM. With Caliburn.Microfor example, you can define your main view as:

如果您使用的是 MVVM ,则应该考虑使用 MVVM 框架。以Caliburn.Micro为例,您可以将主视图定义为:

<TabControl x:Name="Items">
    <TabControl.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding DisplayName}" />
        </DataTemplate>
    </TabControl.ItemTemplate>
</TabControl>

Where the data context is a Conductortype that has a collection. The Itemsproperty will expose a collection of your view models:

其中数据上下文是具有集合的导体类型。该Items属性将公开您的视图模型的集合:

public class MainViewModel : Conductor<IScreen>.Collection.OneActive
{ 
    private OneOfMyViewModels oneOfMyViewModels;

    private AnotherViewModel anotherViewModel;

    protected override void OnInitialise()
    {
        // Better to use constructor injection here
        this.oneOfMyViewModels = new OneOfMyViewModels();
        this.anotherViewModel = new AnotherViewModel();

        this.Items.Add(this.oneOfMyViewModels);
        this.Items.Add(this.anotherViewModel);
    }

    protected override void OnActivate()
    {
        base.OnActivate();
        this.ActivateItem(this.oneOfMyViewModels);
    }
}

public class OneOfMyViewModels : Screen
{
    public OneOfMyViewModels()
    {
        this.DisplayName = "My First Screen";
    }
}

回答by Lawrence

I posted an answer to a different question which shows how to do exactly this: How to Get a Reference to a ViewModel

我发布了一个不同问题的答案,其中显示了如何做到这一点:如何获取对 ViewModel 的引用

It's a very simple example, but hopefully should get you started along the right track.

这是一个非常简单的示例,但希望能让您走上正确的道路。