Articles in this section
Category / Section

How to detect the close event for non-document controls in SfDockingManager

1 min read

This article describes how to detect close event for non-document controls in SfDockingManager.

Triggered the Close Button Click event to detect the non-document windows which are closed in SfDockingManager.

The following code example demonstrates the same.

XAML

<navigation:SfDockingManager x:Name="docking" LayoutUpdated="docking_LayoutUpdated">
   <ContentControl navigation:SfDockingManager.Header="ToolBox" navigation:SfDockingManager.DesiredWidthInDockedMode="300"/>
   <ContentControl navigation:SfDockingManager.Header="Mainpage.xaml" navigation:SfDockingManager.DockState="Document"/>
   <ContentControl navigation:SfDockingManager.Header="App.xaml" navigation:SfDockingManager.DockState="Document"/>
   <ContentControl navigation:SfDockingManager.Header="Solution Explorer" navigation:SfDockingManager.DesiredWidthInDockedMode="300" navigation:SfDockingManager.SideInDockedMode="Right"/>
   <ContentControl navigation:SfDockingManager.Header="Output" navigation:SfDockingManager.SideInDockedMode="Bottom" navigation:SfDockingManager.DesiredHeightInDockedMode="200"/>
</navigation:SfDockingManager>

 

C#

public partial class MainPage : Page
    {
       public MainPage()
       {
          this.InitializeComponent();
       }
       private void Win_Loaded(object sender, RoutedEventArgs e)
       {
          foreach (ToggleButton btn in FindVisualChildrenOfType<ToggleButton>(sender as DockWindow))
          {
             if (btn.Name == "PART_CloseButton")
             {
                btn.Click -= Btn_Click;
                btn.Click += Btn_Click;
             }
          }
          (sender as DockWindow).Loaded -= Win_Loaded;
       }
 
 
       private async void Btn_Click(object sender, RoutedEventArgs e)
       {
          MessageDialog dialog = new MessageDialog("Closed : " + SfDockingManager.GetHeader(docking.ActiveWindow));
          await dialog.ShowAsync();
       }
 
 
       public static IEnumerable<T> FindVisualChildrenOfType<T>(DependencyObject parent) where T : DependencyObject
       {
          List<T> foundChildren = new List<T>();
          int childCount = VisualTreeHelper.GetChildrenCount(parent);
          for (int i = 0; i < childCount; i++)
          {
             var child = VisualTreeHelper.GetChild(parent, i);
             T childType = child as T;
             if (childType == null)
             {
                foreach (var other in FindVisualChildrenOfType<T>(child))
                yield return other;
             }
             else
             {
                yield return (T)child;
             }
          }
       }
 
       private void docking_LayoutUpdated(object sender, object e)
       {
          foreach (DockWindow win in FindVisualChildrenOfType<DockWindow>(docking))
          {
             if (win != null)
             {
                win.Loaded -= Win_Loaded;
                win.Loaded += Win_Loaded;
             }
          }
       }
    }

 

The output for the above code is shown below:

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied