Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
125
Any way to close all splitpane and tabgrouppane programmatically
posted

Hi,

Is there any way to close all splitpane and tabgrouppane programmatically?

I know that ContentPane can be closed by setting CloseAction and  ExecuteCommand(ContentPaneCommands.Close). But how about closing all splitpane and tabgrouppane?

In fact, my actual task is to close all panes before LoadLayout. So that no unkown pane remains in XamDockManager.

Thanks a lot.

Parents
No Data
Reply
  • 54937
    Verified Answer
    Offline posted

    Do you know which panes you want to close? If you do then you could use the GetPanes method to get all the panes, enumerate them to make a list of the ones to remove and then enumerate the list closing the panes you don't want around. Note, make sure you do not close the panes within the GetPanes enumeration since panes could be reparented during the close and that could cause you to miss panes or possible get into a bad state.

    If you don't know which ones you want to remove then you could remove them all, listen to the InitializePaneContent event and hand back the ones that are needed by the layout being loaded. Here's a basic version:

      private static void CloseAndLoadLayout(XamDockManager xdm, string layout)
      {
       var closeProp = ContentPane.CloseActionProperty;
       var knownPanes = new Dictionary<string, ContentPane>();

       // since we're going to manipulate the panes we want to get a snapshot
       // of the panes so we're using ToList
       foreach (ContentPane pane in xdm.GetPanes(PaneNavigationOrder.ActivationOrder).ToList())
       {
        // TODO - are there any panes you want to keep? e.g. ones that have
        // their "IncludeInLayout" set to false?

        var oldCloseAction = pane.ReadLocalValue(closeProp);

        // temporarily change the close action to RemovePane so everything
        // is removed and cleaned up
        pane.CloseAction = PaneCloseAction.RemovePane;

        pane.ExecuteCommand(ContentPaneCommands.Close);

        // restore the old value
        if (oldCloseAction == DependencyProperty.UnsetValue)
         pane.ClearValue(closeProp);
        else if (oldCloseAction is BindingExpressionBase)
         pane.SetBinding(closeProp, ((BindingExpressionBase)oldCloseAction).ParentBindingBase);
        else
         pane.SetValue(ContentPane.AllowCloseProperty, oldCloseAction);

        // figure out which you want to keep...
        if (!string.IsNullOrEmpty(pane.Name))
         knownPanes[pane.Name] = pane;
       }

       // listen for the InitializePaneContent event so we can give back the old panes
       EventHandler<InitializePaneContentEventArgs> handler = delegate(object sender, InitializePaneContentEventArgs e)
       {
        ContentPane oldPane;
        if (knownPanes.TryGetValue(e.NewPane.Name, out oldPane))
        {
         e.NewPane = oldPane;
         knownPanes.Remove(e.NewPane.Name);
        }
       };

       try
       {
        xdm.InitializePaneContent += handler;

        xdm.LoadLayout(layout);
       }
       finally
       {
        xdm.InitializePaneContent -= handler;
       }
      }

Children