find the parent window of an item in a actipro toolwindow

Docking/MDI for WPF Forum

Posted 14 years ago by Arthur Damen
Version: 10.1.0523
I have a procedure to find the parent window, but in the following case it does not work. The toolwindow says it has no parents????

Am i doing something wrong?
The example has been simplified.

<ribbon:RibbonWindow x:Class="MainWindow"
    Title="MainWindow" Height="350" Width="525">
    <DockPanel >
            <Button Width="73" Height="23" Name="rr">click</Button>


  Private Sub rr_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles rr.Click
    Dim Owner As Window = FindParentWindow(Me)
  End Sub

  Public Shared Function FindParentWindow(ByVal CurrentElement As Object) As DependencyObject
    On Error Resume Next
    Dim P As Object = Nothing
    If TypeOf CurrentElement.parent Is ContentControl Then
      P = CurrentElement.parent
      P = VisualTreeHelper.GetParent(CurrentElement)
    End If
    If IsNothing(P) Then
      Return Nothing 'not found, end of tree
      If P.GetType Is GetType(Window) OrElse P.GetType Is GetType(RibbonWindow) OrElse P.GetType.BaseType Is GetType(Window) OrElse P.GetType.IsSubclassOf(GetType(Window)) Then 'found a visual of the desired type!
        Return P
        Return FindParentWindow(P)
      End If
    End If
  End Function

Comments (4)

Posted 14 years ago by Actipro Software Support - Cleveland, OH, USA
Hi Arthur,

The ToolWindow is only responsible for rendering it's tab. The ToolWindowContainer displays the content of the selected ToolWindow. This works much like the native WPF TabControl. One differences is that if the ToolWindowContainer only has 1 ToolWindow, then the tab strip is not shown. This means that the ToolWindow is not going to be part of the visual tree.

The ToolWindowContainer is the logical parent of the ToolWindow. The problem is your code above only walks up the logical tree if the logical parent is a ContentControl. The ToolWindowContainer is not a ContentControl. Switching between walking up the visual tree and the logical tree, probably isn't the best approach as you can run into dead-ends like in this case.

You can use our VisualTreeHelperExtended.GetAncestor method to get the parent Window, as determined by the visual tree. Or you can use the Window.GetWindow method, which is probably what you really need.

Actipro Software Support

Posted 14 years ago by Arthur Damen
thanks for your help. the window.getwindow did the trick.
Posted 11 years ago by Piyush Parsai


In my case both Window.GetWindow(this) and ActiproSoftware.Windows.Media.VisualTreeHelperExtended.GetAncestor(this) are returning as null.

When I recursively trace the parents LogicalTreeHelper.GetParent(node) and VisualTreeHelper.GetParent(node) gives me null for ToolWindow

Can you give me pointers to overcome this 

My code:

<docking:SplitContainer Orientation="Horizontal">
<!--input tree-->
<docking:ToolWindowContainer x:Name="inputTreeContainer" docking:DockSite.ControlSize="75,75">
<docking:ToolWindow x:Name="inputTree" Title="Input"
CanAttach="False" CanDrag="True" HasOptions="True" CanClose="True" CanAutoHide="True"
CanDockLeft="True" CanDockTop="False" CanDockRight="False" CanDockBottom="False"
Background="{StaticResource windowBackground}"
Tag="{x:Static local:InputTree}">

Posted 11 years ago by Actipro Software Support - Cleveland, OH, USA

Hi Piyush,

It's hard to say what's happening in your case without a sample to examine.  But what we previously posted is accurate information.  If you can't figure it out, please make a new simple sample project that shows the problem and email that to our support address.  Reference this post and rename the .zip file extension so it doesn't get spam blocked.

Actipro Software Support

The latest build of this product (v24.1.2) was released 2 months ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.