简体   繁体   English

从WPF / MVVM应用程序的主窗口中打开第二个窗口

[英]Opening second window from main window in WPF/MVVM app

Could you tell me how to in pure MVVM way call (I mean open/show) child window from parent window. 您能告诉我如何从父窗口以纯MVVM方式调用(我的意思是打开/显示)子窗口。 Let's say I have two Views: 假设我有两种观点:

  1. MainWindow.cs (MainWindow.xaml) - parent window (DataContext = new MainWindowViewModel()) MainWindow.cs(MainWindow.xaml)-父窗口(DataContext =新的MainWindowViewModel())
  2. Window.cs (Window.xaml) - child window (DataContext = new WindowViewModel()) Window.cs(Window.xaml)-子窗口(DataContext = new WindowViewModel())

And corresponding ViewModel classes: 以及相应的ViewModel类:

  1. MainWindowViewModel.cs MainWindowViewModel.cs
  2. WindowViewModel.cs WindowViewModel.cs

I would like my window to be opened after button click (button that is on the MainWindow view). 我希望在单击按钮(MainWindow视图上的按钮)后打开窗口。 Because of that I have defined command binding in MainWindow.xaml: 因此,我在MainWindow.xaml中定义了命令绑定:

<Button x:Name="buttonOpenWindow" Content="Open window..." Width="100" Height="20" Command="{Binding OpenWindowCmd}"/>

And MainWindowViewModel.cs piece: 和MainWindowViewModel.cs一块:

public ICommand OpenWindowCmd { get; set; }

public MainWindowViewModel()
{
    OpenWindowCmd = new RelayCommand(o => OpenWindow());
}

private void OpenWindow()
{
    // What to put here?
}

In Window.xaml I added something like that: 在Window.xaml中,我添加了以下内容:

<Window x:Class="Namespace.View.Window"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    xmlns:vm="clr-namespace:Namespace.ViewModel"
    Title="Title" Height="300" Width="325" Visibility="{Binding IsWindowVisible, Converter={StaticResource BooleanToVisibilityConverter}}">
<Window.Resources>
    <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
</Window.Resources>

(...)

And the WindowViewModel.cs: 和WindowViewModel.cs:

using System.ComponentModel;
using System.Runtime.CompilerServices;
using Namespace.Annotations;

namespace Namespace.ViewModel
{
    public class WindowViewModel : INotifyPropertyChanged
    {
        private bool _isWindowVisible;
        public bool IsWindowVisible
        {
            get { return _isWindowVisible; }
            set
            {
                _isWindowVisible = value;
                OnPropertyChanged(nameof(IsWindowVisible));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        [NotifyPropertyChangedInvocator]
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

I am not sure what to do next and if that approach is correct. 我不确定下一步该怎么做以及该方法是否正确。 I found some services implementations in the forum, but I thought of using just Visibility property instead (but not sure if it is possible). 我在论坛上找到了一些服务实现,但是我想到了只使用Visibility属性(但不确定是否可以)。 I need to somehow change the IsWindowVisible in one of the view models I suppose. 我需要以某种方式在我假设的视图模型中更改IsWindowVisible。 Could anyone suggest how to gently handle such sub window opening? 有人可以建议如何轻轻地处理此类子窗口的打开吗?

If I understood well, you need something like this: 如果我理解得很好,则您需要以下内容:

private void OpenWindow()
{
    WindowViewModel wvm = new WindowViewModel();
    Window win = new Window()
    {
        DataContext = wvm;
    };
    win.Show();
}

If you don't like this solution then try the one from the comments with IWindowService. 如果您不喜欢此解决方案,请尝试使用IWindowService注释中的解决方案。 In any case it makes no sense to use a Visibility property. 无论如何,使用Visibility属性是没有意义的。

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

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