Articles in this section
Category / Section

How to detect if a docked or floating window content is visible or not in tabbed state of WPF DockingManager?

2 mins read

In WPF DockingManager, normally the Dock and Float windows are visible. But if the items are Tabbed inside another Dock or Float window, we can get the visible items in Dock and Float window by SelectedContent from InternalTabControl of DockedElementHost. We can get the DockedElementHost of each element using GetHost method. The same has been demonstrated in the following code example:

XAML:

The following code shows of adding the children in the DockingManager.

<Window x:Class="Dockingdemo_154159.MainWindow"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:syncfusion="http://schemas.syncfusion.com/wpf"
                 Title="MainWindow" Height="350" Width="525">
       <Grid>
            <Grid.RowDefinitions>
                 <RowDefinition Height="40"/>
                 <RowDefinition Height="40"/>
                 <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <TextBlock Name="VisibleItems"/>
            <Button x:Name="Button1" Width="100" Height="23" Click="Button1_Click_1" Content="Clicktoget" Grid.Row="1"/>
            <syncfusion:DockingManager  x:Name="Docking1" UseDocumentContainer="True" Grid.Row="2" >
                <ContentControl x:Name="Content1" syncfusion:DockingManager.Header="Dock1" syncfusion:DockingManager.State="Document"/>
                <ContentControl x:Name="Content2" syncfusion:DockingManager.Header="Dock2" syncfusion:DockingManager.State="Document"/>
                <ContentControl x:Name="Content3"  syncfusion:DockingManager.Header="Dock3" syncfusion:DockingManager.State="Document"/>
                <ContentControl x:Name="Content4" syncfusion:DockingManager.Header="Dock4"/>
                <ContentControl x:Name="Content5" syncfusion:DockingManager.Header="Dock5"/>
                <ContentControl x:Name="Content6" syncfusion:DockingManager.Header="Dock6"/>
            </syncfusion:DockingManager>
       </Grid>
</Window>

C#:

using Syncfusion.Windows.Tools.Controls;
namespace Dockingdemo_154159
{
  /// <summary>
  /// Interaction logic for MainWindow.xaml
  /// </summary>
  public partial class MainWindow : Window
  {
    public MainWindow()
    {
      InitializeComponent();
    }
    // Following code shows how to get the collection of DocumentTabControl:
    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 Button1_Click_1(object sender, RoutedEventArgs e)
    {
      VisibleItems.Text = "";
      foreach (DocumentTabControl tabs in FindVisualChildrenOfType<DocumentTabControl>(Docking1))
      {
        foreach (ContentControl content in Docking1.Children)
        {
          if ((tabs.SelectedItem as TabItemExt).Header.ToString() == DockingManager.GetHeader(content).ToString())
          {
            VisibleItems.Text += DockingManager.GetHeader(content).ToString() + "; ";
            // Visible items
            // MessageBox.Show(DockingManager.GetHeader(content).ToString());
          }
          else
          {
            // Not Visible items
          }
        }
      }
      foreach (var child in Docking1.Children)
      {
        if (DockingManager.GetState(child as DependencyObject) == DockState.Float)
        {
          //Getting the dockedelementtabbedhost using the GetHost method.
          DockedElementTabbedHost host = DockingManager.GetHost(child as FrameworkElement, DockState.Float);
          if (host.InternalTabControl != null && host.InternalTabControl.Items.Count > 1)
          {
            if(host.InternalTabControl.SelectedContent == child)
              VisibleItems.Text += DockingManager.GetHeader(host.InternalTabControl.SelectedContent as DependencyObject) + "; ";
          }
          else
            VisibleItems.Text += DockingManager.GetHeader(child as DependencyObject) + "; ";
        }
        else if (DockingManager.GetState(child as DependencyObject)== DockState.Dock)
        {
         DockedElementTabbedHost host = DockingManager.GetHost(child as FrameworkElement, DockState.Dock);
         if (host.InternalTabControl != null && host.InternalTabControl.Items.Count > 1 )
         {
            if (host.InternalTabControl.SelectedContent == child)
              VisibleItems.Text += DockingManager.GetHeader(host.InternalTabControl.SelectedContent as DependencyObject) + "; ";
         }
         else
           VisibleItems.Text += DockingManager.GetHeader(child as DependencyObject) + "; ";
        }
      }
    }
  }
}

The following output shows the visible elements of the DockingManager in the textbox.

WPF DockingManager displays visible elements in textbox

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