find the parent window of an item in a actipro toolwindow

Docking/MDI for WPF Forum

Posted 9 years ago by Arthur Damen
Version: 10.1.0523
Avatar
Hi,
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"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:docking="http://schemas.actiprosoftware.com/winfx/xaml/docking"
    xmlns:local="clr-namespace:WpfApplication1"
    xmlns:ribbon="http://schemas.actiprosoftware.com/winfx/xaml/ribbon"
    Title="MainWindow" Height="350" Width="525">
    <Grid>
    <DockPanel >
      <docking:ToolWindowContainer>
        <docking:ToolWindow>
          <Grid>
            <Button Width="73" Height="23" Name="rr">click</Button>
          </Grid>
        </docking:ToolWindow>
      </docking:ToolWindowContainer>
    </DockPanel>

  </Grid>
</ribbon:RibbonWindow>

  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
    Else
      P = VisualTreeHelper.GetParent(CurrentElement)
    End If
    If IsNothing(P) Then
      Return Nothing 'not found, end of tree
    Else
      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
      Else
        Return FindParentWindow(P)
      End If
    End If
  End Function

Comments (4)

Posted 9 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
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 9 years ago by Arthur Damen
Avatar
thanks for your help. the window.getwindow did the trick.
Posted 6 years ago by Piyush Parsai
Avatar

Hi,

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"
CanBecomeDocument="False"
CanDockLeft="True" CanDockTop="False" CanDockRight="False" CanDockBottom="False"
Background="{StaticResource windowBackground}"
Tag="{x:Static local:InputTree}">
</docking:ToolWindow>
</docking:ToolWindowContainer>

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

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 (v2018.1 build 0675) was released 1 month ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.