wpf 从视图模型中获取窗口

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

Getting Window from view model

c#wpfmvvm

提问by Ralph

I have created a custom messageBox Window to replace the typical MessageBox.

我创建了一个自定义的 messageBox 窗口来替换典型的 MessageBox。

My custom messageBox (child Window) needs parent window to be passed as an argument. The parent window is where child window will be located within in the location specified (Top Left, Top Center, etc.) also as an argument.

我的自定义消息框(子窗口)需要将父窗口作为参数传递。父窗口是子窗口位于指定位置(左上、中上等)的位置,也作为参数。

So when calling my custom messageBox from view model, I need to get the Window to pass it through. How can I get the Window associated to the view model?

因此,当从视图模型调用我的自定义 messageBox 时,我需要让 Window 传递它。如何获取与视图模型关联的 Window?

Maybe using an Interface like commented here? I am trying to implement it but this.DataContext.View does not exist.

也许使用像这里评论的接口?我正在尝试实现它,但 this.DataContext.View 不存在。

I am using Visual Studio 2008.

我正在使用 Visual Studio 2008。

ATTEMPT #1: mm8 solution

尝试 #1:mm8 解决方案

<Window x:Class="MyNamespace.Main"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:viewmodel="clr-namespace:MyNamespace.ViewModels">
    <Window.Resources>
        <viewmodel:MainViewModel x:Key="wMainViewModel" />
    </Window.Resources>

    <Grid DataContext="{StaticResource wMainViewModel}">
    </Grid>
</Window>

Taken into account that I initialize DataContext from xaml and not in code-behind from constructor, how can I pass the view to the view model??

考虑到我从 xaml 初始化 DataContext 而不是在构造函数的代码隐藏中,如何将视图传递给视图模型?

回答by mm8

How can I get the Window associated to the view model?

如何获取与视图模型关联的 Window?

You could inject the view model with an interface that the window implements:

您可以使用窗口实现的接口注入视图模型:

public interface IView
{
    double Top { get; }
    double Left { get; }
    double Height { get; }
    double Width { get; }
}

public partial class MainWindow : Window, IView
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = new ViewModel(this);
    }
}

public class ViewModel
{
    private readonly IView _view;

    public ViewModel(IView view)
    {
        _view = view;
    }

    //...
}

Then the view model knows only about an interface (which maybe should be called something else than IView).

然后视图模型只知道一个接口(它可能应该被称为 IView 以外的其他东西)。

If you want an ugly and non-MVVM friendly shortcut, you could use the Applicationclass:

如果你想要一个丑陋且非 MVVM 友好的快捷方式,你可以使用这个Application类:

var win = Application.Current.MainWindow;

A factory class to create everything:

创建一切的工厂类:

public MainWindow WindowFactory()
{
    MainWindow mainWindow = new MainWindow();
    ViewModel viewModel = new ViewModel();
    mainWindow.DataContext = viewModel;

    return mainWindow;
}