Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
 GetTemplateChild Method

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
FrameworkElement..::.GetTemplateChild Method

Do not use. Use FindName instead.

Namespace:  System.Windows
Assembly:  PresentationFramework (in PresentationFramework.dll)
Visual Basic (Declaration)
Protected Friend Function GetTemplateChild ( _
    childName As String _
) As DependencyObject
Visual Basic (Usage)
Dim childName As String
Dim returnValue As DependencyObject

returnValue = Me.GetTemplateChild(childName)
C#
protected internal DependencyObject GetTemplateChild(
    string childName
)
Visual C++
protected public:
DependencyObject^ GetTemplateChild(
    String^ childName
)
JScript
protected internal function GetTemplateChild(
    childName : String
) : DependencyObject
XAML
You cannot use methods in XAML.

Parameters

childName
Type: System..::.String
Name of the child to find.

Return Value

Type: System.Windows..::.DependencyObject
The requested element. May be nullNothingnullptra null reference (Nothing in Visual Basic) if no element of the requested name exists.

FindName supplies the same function as this method. FindName is public instead of protected, and it uses correct name-scoping considerations that allow it to access the template within an element and find named items within it.

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
**Do** use GetTemplateChild within OnApplyTemplate(); Don't use FindNames      ChrisLaMont   |   Edit   |   Show History

Even though the XML documentation says use FindName instead, that doen't work when inside OnApplyTemplate(). Here I was trying to reference a PART_ slider in a custom control.

Here is a sample XAML that will cause FindNames to fail and return Null:


<Window.Resources>
<ControlTemplate x:Key="FancyColorPickerTemplate">
<Border Background="LightBlue"
BorderBrush="LightSeaGreen"
BorderThickness="1">
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.Resources>
<Style TargetType="{x:Type Slider}">
<Setter Property="Orientation" Value="Vertical"></Setter>
<Setter Property="TickPlacement" Value="TopLeft"></Setter>
<Setter Property="TickFrequency" Value="50"></Setter>
<Setter Property="Minimum" Value="0"></Setter>
<Setter Property="Maximum" Value="255"></Setter>
<Setter Property="Margin" Value="5"></Setter>
</Style>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Margin" Value="3"></Setter>
<Setter Property="FontSize" Value="10"></Setter>
</Style>
</Grid.Resources>
<Ellipse Grid.Column="0" Grid.RowSpan="2"
Margin="10" Height="120" Stroke="LightGray" StrokeThickness="5">
<Ellipse.Fill>
<SolidColorBrush Color="{Binding Path=Color,
RelativeSource={RelativeSource TemplatedParent}}"></SolidColorBrush>
</Ellipse.Fill>
</Ellipse>

<Slider Name="PART_RedSlider" Grid.Column="1"></Slider>
<TextBlock Grid.Row="1" Grid.Column="1">RED</TextBlock>
<Slider Name="PART_GreenSlider" Grid.Column="2"></Slider>
<TextBlock Grid.Row="1" Grid.Column="2">GREEN</TextBlock>
<Slider Name="PART_BlueSlider" Grid.Column="3"></Slider>
<TextBlock Grid.Row="1" Grid.Column="3">BLUE</TextBlock>

</Grid>
</Border>
</ControlTemplate>

</Window.Resources>

Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker