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


hi,
set the DefaultStyleKey at constructor
DefaultStyleKey = typeof(ur type)
Its new at beta 2
BR,
ashraful.asif
ashraful.asif
June 10, 2008
Thanks for the pointer
lee
June 10, 2008
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.
diego
June 10, 2008
glad it worked.
lee
lee
June 10, 2008
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
Jeff
June 11, 2008
Thanks for the workaround. I will try this and see
lee
June 11, 2008
Thanks Jeff, it worked
lee
June 11, 2008
[...] Jeff’s workaround seemed to have worked [...]
ComboBox code updated to Beta2 « Lee’s Corner
June 11, 2008
[...] 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 [...]
Silverlight Cream for June 10, 2008 -- #296
June 17, 2008