【问题标题】:Pass data from parent window to child window and back to parent window WPF C# [duplicate]将数据从父窗口传递到子窗口并返回父窗口WPF C# [重复]
【发布时间】:2014-08-01 15:48:01
【问题描述】:

我有 2 个 WPF 窗口,MainWindow 和 ChildWindow。我想在主窗口中点击一个事件来打开一个子窗口(我会将一些信息传递给子窗口)。在子窗口中,我希望 MainWindow 在子窗口中单击时更新。 工作流程: MainWindow --> TextBlock click --> create New ChildWindow params: x_coord, y_coord, line number --> 显示子窗口 子窗口 --> 在提交 btn 时单击 --> 将文本作为字符串发送回主窗口

public MainWindow()
{
    InitializeComponent();
    WorkspaceVersion.MouseLeftButtonDown += new MouseButtonEventHandler(WorkspaceVersion_MouseLeftButtonDown); // text block click event
}

// mouse left button down 
private void WorkspaceVersion_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    Point position = e.GetPosition(WorkspaceVersion);
    var i = Math.Round(position.Y/15); // line number
    var commentWindow = new CommentWindow(position, i);
    commentWindow.Show();
}

///////////////  child window /////////////
// probably need to create a custom event to raise, then register to in mainwindow
public CommentWindow()
{
    InitializeComponent();
}

protected void SubmitButton_OnClick(object sender, EventArgs e)
{
    string comment = new TextRange(CommentBox.Document.ContentStart, CommentBox.Document.ContentEnd).Text;
    // raise event with custom args????
    // main window should be listening for this event??
    this.Close();
 }

【问题讨论】:

    标签: c# wpf window


    【解决方案1】:

    您可以在这里做几件事。首先,我将研究 MVVM 模式,您可以使用该模式对窗口进行建模,然后在视图模型之间传递数据。

    如果您想保留代码隐藏,您可以简单地将注释公开为子窗口的公共属性,然后您的主窗口可以访问该属性。特别是如果您想将子窗口显示为模式。否则,在非模式设置中,您可以在子窗口上创建一个事件,该事件将在单击提交按钮时引发并包含评论。如果您这样做,请记住在不再需要子窗口时取消订阅该事件。

    【讨论】:

      猜你喜欢
      • 2015-11-13
      • 1970-01-01
      • 2014-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多