Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 窗口关闭WPF-MVVM后返回值_C#_Wpf_Mvvm - Fatal编程技术网

C# 窗口关闭WPF-MVVM后返回值

C# 窗口关闭WPF-MVVM后返回值,c#,wpf,mvvm,C#,Wpf,Mvvm,我有一个用户控件,其中只包含一个文本框和保存按钮。我将此用户控件显示为对话框窗口。用户在文本框中输入注释并单击保存按钮后,我将关闭对话框窗口 我成功地做到了这一点。我的问题是我想把文本框值传递到主窗口。我怎么能通过这个?这是我的密码 //显示窗口 var window = new RadWindow { Owner = Application.Current.MainWindow, WindowStartupLoca

我有一个用户控件,其中只包含一个文本框和保存按钮。我将此用户控件显示为对话框窗口。用户在文本框中输入注释并单击保存按钮后,我将关闭对话框窗口

我成功地做到了这一点。我的问题是我想把文本框值传递到主窗口。我怎么能通过这个?这是我的密码

//显示窗口

 var window = new RadWindow
           {
              Owner = Application.Current.MainWindow,
              WindowStartupLocation = WindowStartupLocation.CenterOwner
           };          
        window.Content = control;
        window.SizeToContent = true;
        window.Header = header;  
        window.ShowDialog()
使用ICommand关闭ViewModel中的窗口

private void SaveCommentExecute()
    {
        var window = Application.Current.Windows.OfType<Window>().SingleOrDefault(x => x.IsActive);
        if (window != null)
        {
            window.Close();
        }
        // get comments and pass back to main window
    }
private void SaveCommentExecute()
{
var window=Application.Current.Windows.OfType().SingleOrDefault(x=>x.IsActive);
如果(窗口!=null)
{
window.Close();
}
//获取评论并传回主窗口
}

您使用的是
ShowDialog
,但它没有任何出色的功能

在显示对话框的类中:

if (window.ShowDialog() && window.DialogResult.Value == true)
{
    //Access the properties on the window that hold your data.
}
window.ShowDialog();
string value = control.TheValue;
然后在对话框中:

if (window != null)
{
    this.DialogResult = true;
    //Set the properties to the data you want to pass back.
    window.Close();
}

只需通过控件上的属性公开值:

public string TheValue
{
    get { return theTextBox.Text; }
}
并从显示对话框的位置阅读:

if (window.ShowDialog() && window.DialogResult.Value == true)
{
    //Access the properties on the window that hold your data.
}
window.ShowDialog();
string value = control.TheValue;

(不确定您为什么将问题标记为“MVVM”,因为您发布的代码似乎不符合MVVM模式)

有多种方法可以做到这一点,internet上有大量关于此主题的资源。你的案例有什么特别之处???@walther我还没有看到用户控制的例子提示:如果你对MVVM很认真的话,你需要一个对话服务。然后,模型使用该服务创建对话框并捕获显示对话框的返回代码。我必须在哪个viewmodel中执行此操作?这是针对Windows窗体的,而不是WPF。而且它没有回答OP的问题。@ThomasLevsque我为WPF修改了它,但在不知道需要保存哪些数据的情况下,我不能建议将属性添加到对话框中。也许OP可以创建一个属性,返回VM的
DataContext
。@Chatra您的viewmodels在哪里。你没有在你的帖子中显示任何内容。