wpf 在 ViewModel 中创建视图对象

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

Create view object in ViewModel

c#wpfmvvmcommandbinding

提问by Ramesh

I have the following code in my C# WPF MVVM application.

我的 C# WPF MVVM 应用程序中有以下代码。

public RelayCommand PolishCommand
    {
        get
        {
            polishcommand = new RelayCommand(e =>
            {

                PolishedWeightCalculatorViewModel model = new PolishedWeightCalculatorViewModel(outcomeIndex, OutcomeSelectedItem.RoughCarats);
                PolishedWeightCalculatorView polish = new PolishedWeightCalculatorView(model);
                bool? result = polish.ShowDialog();
                if (result.HasValue)
                {

But i came to know that, calling a window from viewmodel is wrong one in MVVM pattern.

但我开始知道,在 MVVM 模式中,从视图模型调用窗口是错误的。

Also stated in the below link.

也在下面的链接中说明。

M-V-VM Design Question. Calling View from ViewModel

MV-VM 设计问题。从 ViewModel 调用视图

Please help me anybody by providing an alternate solution.

请通过提供替代解决方案来帮助我。

Thanks in advance.

提前致谢。

回答by Sheridan

You are right that generally you should neveraccess views from view models. Instead in WPF, we set the DataContextproperty of the view to be an instance of the relating view model. There are a number of ways to do that. The simplest but least correct is to create a new WPF project and put this into the constructor of MainWindow.xaml.cs:

你是正确的,通常你应该永远不会从视图模型访问视图。相反,在 WPF 中,我们将DataContext视图的属性设置为相关视图模型的实例。有很多方法可以做到这一点。最简单但最不正确的是创建一个新的 WPF 项目并将其放入以下构造函数中MainWindow.xaml.cs

DataContext = this;

In this instance the 'view model' would actually be the code behind for the MainWindow'view'... but then the view and view model are tied together and this is what we try to avoid by using MVVM.

在这种情况下,“视图模型”实际上是MainWindow“视图”背后的代码……但是视图和视图模型被捆绑在一起,这是我们通过使用 MVVM 试图避免的。

A better way is to set the relationship in a DataTemplatein the Resourcessection (I prefer to use App.Resourcesin App.xaml:

更好的方法是DataTemplateResources节中的 a中设置关系(我更喜欢使用App.Resourcesin App.xaml

<DataTemplate DataType="{x:Type ViewModels:YourViewModel}">
    <Views:YourView />
</DataTemplate>

Now wherever you 'display' a view model in the UI, the relating view will automatically be shown instead.

现在,无论您在 UI 中“显示”视图模型的何处,都会自动显示相关视图。

<ContentControl Content="{Binding ViewModel}" />

A third way is to create an instance of the view model in the Resourcessection like so:

第三种方法是在部分中创建视图模型的实例,Resources如下所示:

<Window.Resources>
    <ViewModels:YourViewModel x:Key="ViewModel" />
</Window.Resources>

You can then refer to it like so:

然后你可以像这样引用它:

<ContentControl Content="{Binding Source={StaticResource ViewModel}}" />

回答by Lawrence

I have answered a very similar question previously, which details how you can open a new window from your view model, whilst maintaining the separation of concerns that the MVVM pattern promotes. I hope this helps: Open a new Window in MVVM

我之前回答过一个非常相似的问题,其中详细说明了如何从视图模型中打开一个新窗口,同时保持 MVVM 模式所促进的关注点分离。我希望这会有所帮助:在 MVVM 中打开一个新窗口

回答by Oyyou

You are allowed to break the rule. You don't have to follow MVVM completely. I am always using commands to create a new view. You could even create an event (Amagosh, did he just say that!?) for when you click on a button. I mean, this is just my opinion, I guess it depends on the style programming you're into.

你可以打破规则。您不必完全遵循 MVVM。我总是使用命令来创建新视图。您甚至可以在单击按钮时创建一个事件(Amagosh,他是不是这么说的!?)。我的意思是,这只是我的意见,我想这取决于您所采用的编程风格。