Tristilii
Tristilii

Reputation: 1

GetTemplateChild Always Returns Null

I usually like to solve problems by myself but I can't really wrap my head around the following:


    <Style TargetType="{x:Type local:PanelItemBase}"
           BasedOn="{StaticResource {x:Type ContentControl}}"
           >
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:PanelItemBase}">
                    <Grid   x:Name="PART_grid"
                            Width="{Binding Width, RelativeSource={RelativeSource TemplatedParent}}"
                            Height="{Binding Height, RelativeSource={RelativeSource TemplatedParent}}"
                            >
                        <local:PathBase x:Name="PART_pathBase"
                                        Content="{Binding Content, RelativeSource={RelativeSource TemplatedParent}}"
                                        Width="{Binding Width, RelativeSource={RelativeSource TemplatedParent}}"
                                        Height="{Binding Height, RelativeSource={RelativeSource TemplatedParent}}"
                                        CornerRadius="{Binding CornerRadius, RelativeSource={RelativeSource TemplatedParent}}"
                                        Fill="{Binding Fill, RelativeSource={RelativeSource TemplatedParent}}"
                                        Stroke="{Binding Stroke, RelativeSource={RelativeSource TemplatedParent}}"
                                        StrokeThickness="{Binding StrokeThickness, RelativeSource={RelativeSource TemplatedParent}}"
                                        Shadow="{Binding Shadow, RelativeSource={RelativeSource TemplatedParent}}"
                                        ShadowDepth="{Binding ShadowDepth, RelativeSource={RelativeSource TemplatedParent}}"
                                        ShadowDistance="{Binding ShadowDistance, RelativeSource={RelativeSource TemplatedParent}}"
                                        />
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
 

Whenever I try retrieving template parts by calling GetTemplateChild inside the OnApplyTemplate Override like this:

public override void OnApplyTemplate()
{
    base.OnApplyTemplate();
    
    _pathBase = GetTemplateChild("PART_pathBase") as PathBase;
    if (_pathBase == null) Debug.WriteLine("PART_pathBase not found in PanelItemBase template.");
    else Debug.WriteLine("PART_pathBase successfully retrieved.");
    
    _grid = GetTemplateChild("PART_grid") as Grid;
    if (_grid == null) Debug.WriteLine("PART_grid not found in PanelItemBase template.");
    else Debug.WriteLine("PART_grid successfully retrieved.");
}

it always returns null, no matter the template part.

I have tried:

Thanks for any answers in advance!

Upvotes: 0

Views: 43

Answers (0)

Related Questions