/// <summary> /// Show the MDI child with the passed handle. /// Opening event will be raised on the child. /// If the MDI child is already open (Show called previously), /// then it will be Activated. /// </summary> /// <param name="child"></param> public void Show(IMDIChild childItem) { MDIChild child = childItem as MDIChild; // sanity check if (child == null) { throw new ArgumentException("Show child cannot be null"); } // check if mdi child has been shown previously if (mdiChildren.Contains(child)) { // activate it Activate(child); return; } // add to children mdiChildren.Add(child); mdiParent.Items.Add(child.TabItem); // bring to front mdiParent.SelectedItem = child.TabItem; // raise opening event child.RaiseOpening(); }
/// <summary> /// Close the MDI child with the passed handle. /// Closing and Closed events will be raised on child. /// Close can be cancelled by handling the Closing event Cancel property. /// </summary> /// <param name="child"></param> public void Close(IMDIChild childItem) { MDIChild child = childItem as MDIChild; // sanity check if (child == null) { throw new ArgumentException("Activate child cannot be null"); } if (!mdiChildren.Contains(child)) { throw new InvalidOperationException("Cannot call Close on a child which has not been Show(n)"); } // invoke Closing event CancelEventArgs args = new CancelEventArgs(); child.RaiseClosing(args); // check if close has been aborted if (args.Cancel) { return; } // close/remove mdiChildren.Remove(child); mdiParent.Items.Remove(child.TabItem); // raise Closed event child.RaiseClosed(); }
// invoked when TabItem header close X button is clicked private void cmdTabItemCloseButton_Click(object sender, RoutedEventArgs e) { // see XAML, tabitem is bound to Tag property TabItem tabItem = ((Button)sender).Tag as TabItem; MDIChild child = null; // find the MDIChild corresponding to this TabItem foreach (MDIChild c in mdiChildren) { if (c.TabItem == tabItem) { child = c; break; } } // sanity check if (child == null) { // fail-safe - we assume that this TabItem was never // added as a MDI child // so we just remove it mdiParent.Items.Remove(tabItem); return; } // invoke close on the child Close(child); }
/// <summary> /// Bring to front the MDI child with the passed handle. /// </summary> /// <param name="child"></param> public void Activate(IMDIChild childItem) { MDIChild child = childItem as MDIChild; // sanity check if (child == null) { throw new ArgumentException("Activate child cannot be null"); } if (!mdiChildren.Contains(child)) { throw new InvalidOperationException("Cannot call Activate on a child which has not been Show(n)"); } // activate mdiParent.SelectedItem = child.TabItem; }