Accessing WPF Template for Custom Control from Code behind

Posted by Ashwani Mehlem on Stack Overflow See other posts from Stack Overflow or by Ashwani Mehlem
Published on 2010-04-27T09:40:02Z Indexed on 2010/04/27 9:43 UTC
Read the original article Hit count: 341

Filed under:
|
|

Hi,

i am trying to access a named grid inside a default template for a custom control from code behind.
But it seems that the template for the control is null, even after calling ApplyTemplate().
Is that impossible inside the controls constuctor?
Here's the code:

Generic.xaml:
...
<ControlTemplate TargetType="{x:Type local:TimeTableControl}">
    <Grid Name="ContentGrid">
    </Grid>
</ControlTemplate>
...

TimeTableControl.cs:

public TimeTableControl()
{
    ApplyTemplate();
    contentGrid = (Grid)(Template.FindName("ContentGrid", this));  
     //Line above causes null-pointer-exception
     ...
}

© Stack Overflow or respective owner

Related posts about wpf

Related posts about customcontrol