GetTemplateChild Issue- SLBeta2

Here is another one of those things that might have changed in Beta2. I am not sure what/how should be changed as this was working in Beta1

Sometimes I get the following error

and sometimes Object Reference set to null, I was able to narrow it down to GetTemplateChild in OnApplyTemplate. All the calls to this method return null.

 public override void OnApplyTemplate()
            _dropDownButton = GetTemplateChild(“ButtonElement”) as Button;
         _cp = GetTemplateChild(“ContentSite”) as ContentPresenter;
         _border = GetTemplateChild(“border”) as Border;

         _border.Visibility = Visibility.Collapsed          
            _cp.ContentTemplate = ItemTemplate;
            if (_dropDownButton != null)
                _dropDownButton.MouseLeftButtonDown += new MouseButtonEventHandler(_dropDownButton_MouseLeftButtonDown);
                _dropDownButton.IsTabStop = false;
            _rootGrid = GetTemplateChild(“RootElement”) as Grid;            if (_rootGrid != null)           
                _rootGrid.SizeChanged += new SizeChangedEventHandler(RootGrid_SizeChanged);


Here is the source

Please leave a comment of any solutions/workarounds

Update: Adding DefaultStyleKey=typeof(ComboBox) and DefaultStyleKey=typeof(ComboBoxItem) in the respective Constructors should solve this issue. this is New in Beta2

But I still cant get it to work, I get another exception

Update: If we replace ContentPresenter in ControlTemplates with ContentControl all is good and the issue is resolved (This is workaround for a known bug)


9 thoughts on “GetTemplateChild Issue- SLBeta2

  1. hi,

    set the DefaultStyleKey at constructor
    DefaultStyleKey = typeof(ur type)

    Its new at beta 2


  2. thanks! It worked ok for me, I only changed

    //_lbi = (ListBoxItem)lstValues.SelectedItem;
    //string s = (string)(_lbi).Content;
    txtValue.Text = lstValues.SelectedItem.ToString();

    since SelectedItem is an stirng (object with a string) in my case.

  3. Hello,

    Please find below the solution for your second bug. the workaround have been plublished on tap newsgroups by Hristo Hristov :

    “My guess is that you are using ContentPresenter in some of your controls and
    they do not inherit from ContentControl. This is posted as a bug. At the
    moment you can use ContentPresenter only in ContentControl and its
    The current solution (workaround) is to use ContentControl instead of
    ContentPresenter. What I mean is that where you use ContentPresenter in your
    ControlTemplate, you should be using ContentControl instead.”

    Hope this help.



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s