-1

I have 2 WPF windows, MainWindow & ChildWindow. I would like a click event in the mainwindow to open a childwindow (I will be passing some information to the child window). In the Child Window I would like the MainWindow to update on Submit on Click in Child Window. WorkFlow: MainWindow --> TextBlock click --> create New ChildWindow params: x_coord, y_coord, line number --> show child window child window --> on submit btn click --> send text as string back to main window

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();
 }
Matthew Knudsen
  • 213
  • 1
  • 7
  • 19

1 Answers1

0

There are a couple of things you can do here. First off I would look into the MVVM pattern which you could use to model windows and then pass the data between the view models.

If you want to stay with code-behind you can simply expose the comment as a public property of the child window and then your main window can access the property. Especially if you want to show the child window as modal. Otherwise, in a non-modal setting, you can create an event on the child window that will be raised whenever the submit button is clicked and will contain the comment. If you do this remember to unsubscribe from the event once the child window is no longer needed.

Slugart
  • 4,535
  • 24
  • 32