Home > Silverlight > GetTemplateChild Issue- SLBeta2

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)

Categories: Silverlight
  1. ashraful.asif
    June 10, 2008 at 10:27 am | #1

    hi,

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

    Its new at beta 2

    BR,
    ashraful.asif

  2. lee
    June 10, 2008 at 12:19 pm | #2

    Thanks for the pointer

  3. diego
    June 10, 2008 at 4:13 pm | #3

    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.

  4. lee
    June 10, 2008 at 5:28 pm | #4

    glad it worked.

    lee

  5. Jeff
    June 11, 2008 at 6:30 am | #5

    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

  6. lee
    June 11, 2008 at 7:56 am | #6

    Thanks for the workaround. I will try this and see

  7. lee
    June 11, 2008 at 8:11 am | #7

    Thanks Jeff, it worked

  1. June 11, 2008 at 8:22 am | #1
  2. June 17, 2008 at 10:24 pm | #2