Can't access templated views

Docking/MDI for WPF Forum

Posted 12 years ago by Jack
Version: 12.1.0560


I have a DockSite set up with the following style:

<DataTemplate x:Key="taskTemplate">
<tbviews:TaskView x:Name="taskView" />

<!-- Document tabs -->
<Style x:Key="DocumentItemStyle" TargetType="{x:Type apdocking:DocumentWindow}">
<Setter Property="Title" Value="{Binding Label}" />
<Setter Property="ContentTemplate" Value="{StaticResource taskTemplate}" />

 Declared as follows (omitting some of the irrelevant parts):

DocumentItemsSource="{Binding Path=Tasks}"
DocumentItemContainerStyle="{DynamicResource DocumentItemStyle}"
<apdocking:TabbedMdiHost Name="contentContainer" />

 This works great, for the most part - with the help of an attached behavior, the DocumentWindow's show up with the <tbviews:TaskView /> UserControl, and all functions as expected. However, I can't figure out how to access parts of the templated UserControl in code this way. The following code:

DockSite dockSite = (DockSite)FindName("dockSite"); // works fine
DocumentWindow taskWindow = dockSite.ContainerFromDocumentItem(task); // works fine

ContentPresenter myContentPresenter = taskWindow.FindVisualChild<ContentPresenter>(); // works fine
DataTemplate myDataTemplate = myContentPresenter.ContentTemplate; // works fine
object o1 = myDataTemplate.FindName("taskView", myContentPresenter); // returns null

 doesn't work as expected - specifically, the last statement returns 'null', when I expect it to return a reference to the templated TaskView object. 

Is there something that I'm missing? What's the correct way to get a reference to a templated object inside a DocumentWindow?


Comments (3)

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

Hi Jack,

If you have the ContentPresenter ok, I'd think that you could just use VisualTreeHelper methods like GetChild to access the child element that was created from the template.

Actipro Software Support

Posted 12 years ago by Jack

Thanks for your response. Unfortunately, the visual tree doesn't contain what I need. The visual tree hierarchy right below DockingWindow is as follows:

  - ActiproSoftware.Windows.Controls.PixelSnapper ()
    - System.Windows.Controls.Grid ()
      - ActiproSoftware.Windows.Controls.Primitives.TintableBorder (border)
      - System.Windows.Controls.DockPanel ()
        - System.Windows.Controls.StackPanel (buttonContainer)
          - System.Windows.Controls.Button (closeButton)
            - System.Windows.Controls.Border (outerBorder)
              - System.Windows.Controls.Grid ()
                - System.Windows.Controls.Border (innerBorder)
                - ActiproSoftware.Windows.Controls.PixelSnapper ()
                  - System.Windows.Controls.ContentPresenter (presenter)
                    - System.Windows.Controls.Canvas ()
                      - System.Windows.Shapes.Path ()
        - System.Windows.Controls.Image (contextImage)
        - ActiproSoftware.Windows.Controls.PixelSnapper (contentContainer)
          - System.Windows.Controls.DockPanel ()
            - System.Windows.Controls.Image (image)
            - ActiproSoftware.Windows.Controls.PixelSnapper ()
              - System.Windows.Controls.Grid ()
                - System.Windows.Controls.Grid (header)
                  - System.Windows.Controls.TextBlock (title)
                  - System.Windows.Controls.ContentPresenter (contentPresenter)
                - System.Windows.Controls.TextBox (PART_TitleTextBox)

The templated control contains considerably more than this, yet no part of the control is present in the visual tree. What other options are there?

[Modified 12 years ago]

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

Hi Jack,

Sorry but you might have the wrong ContentPresenter there.  If you try and find it within the DocumentWindow, then you'll probably get one within the tab representation of the docking window since the tab is the UI for the docking window.

What you'll want to do is get the dockingWindow.Content property, which likely will be a ContentPresenter.  And use VisualTreeHelper on that.

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.