GetTemplateChild Issue- SLBeta2

Posted on June 9, 2008. Filed under: Silverlight |

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()
        {
            base.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)

Make a Comment

Make A Comment: ( 9 so far )

blockquote and a tags work here.

9 Responses to “GetTemplateChild Issue- SLBeta2”

RSS Feed for Lee’s corner Comments RSS Feed

hi,

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

Its new at beta 2

BR,
ashraful.asif

Thanks for the pointer

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.

glad it worked.

lee

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
descendants.
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.

Regards

Jeff

Thanks for the workaround. I will try this and see

Thanks Jeff, it worked

[...]  Jeff’s workaround seemed to have worked [...]

[...] on the Silverlight WebServices Team is a very nice write-up with links about SL2B2 WebServices. GetTemplateChild Issue- SLBeta2 Lee has apparently found another issue… this one is with the GetTemplaeChild, and has screenshots [...]


Where's The Comment Form?

Liked it here?
Why not try sites on the blogroll...