【发布时间】:2015-05-04 18:24:55
【问题描述】:
我有一个用户控件,它只包含一个文本框和保存按钮。我将此用户控件显示为对话框窗口。用户在文本框中输入 cmets 并单击保存按钮后,我将关闭对话框窗口。
我成功了。我的问题是我想将文本框值传递给主窗口。我怎么能通过这个?这是我的代码
//显示窗口
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
}
【问题讨论】:
-
有多种方法可以做到这一点,互联网上有大量关于这个主题的资源。您的案例有何特别之处??
-
@walther 我还没有看到用户控制的例子
-
提示:如果你对 MVVM 很认真,你需要一个对话服务。然后模型使用该服务来创建对话框并从显示它时捕获返回代码。