窗口关闭WPF-MVVM后的返回值

时间:2021-08-12 04:14:29

I am having a User Control which contains only one textbox and save button. I am showing this user control as Dialog window. After user enter comments in textbox and click on save button, I am closing the dialog window.

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

I am succesful doing this. My issue is I want to pass the textbox value to the main window. How can I pass this ? Here is my Code

我这样做是成功的。我的问题是我想将文本框值传递给主窗口。我怎么能通过这个?这是我的代码

//Showing the Window

//显示窗口

 var window = new RadWindow
           {
              Owner = Application.Current.MainWindow,
              WindowStartupLocation = WindowStartupLocation.CenterOwner
           };          
        window.Content = control;
        window.SizeToContent = true;
        window.Header = header;  
        window.ShowDialog()

Closing the Window in ViewModel using ICommand

使用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
    }

2 个解决方案

#1


Just expose the value through a property on your control:

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

public string TheValue
{
    get { return theTextBox.Text; }
}

And read it from where you show the dialog:

并从显示对话框的位置读取它:

window.ShowDialog();
string value = control.TheValue;

(not sure why you tagged your question "MVVM", because the code you posted doesn't seem to follow the MVVM pattern)

(不确定为什么你标记了你的问题“MVVM”,因为你发布的代码似乎没有遵循MVVM模式)

#2


You are using ShowDialog, but not any of the wonderful features it has...

您正在使用ShowDialog,但它没有任何精彩的功能......

In the class that shows the dialog:

在显示对话框的类中:

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

And then in the dialog itself:

然后在对话框中:

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

#1


Just expose the value through a property on your control:

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

public string TheValue
{
    get { return theTextBox.Text; }
}

And read it from where you show the dialog:

并从显示对话框的位置读取它:

window.ShowDialog();
string value = control.TheValue;

(not sure why you tagged your question "MVVM", because the code you posted doesn't seem to follow the MVVM pattern)

(不确定为什么你标记了你的问题“MVVM”,因为你发布的代码似乎没有遵循MVVM模式)

#2


You are using ShowDialog, but not any of the wonderful features it has...

您正在使用ShowDialog,但它没有任何精彩的功能......

In the class that shows the dialog:

在显示对话框的类中:

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

And then in the dialog itself:

然后在对话框中:

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