none
onApplyTemplate not getting called for my custom control

    Question

  •  Hi,

    I have defined a custom control called SampleControl and I am calling this SampleControl from another file called Parent class.

    The parent class is derived from UserControl.

    and put the styling in xaml. The code snippet is given below

     public class SampleControl : Control
        {
            private Grid panel;
            
            public SampleControl()
            {            
                this.DefaultStyleKey = typeof(SampleControl);           
                this.ApplyTemplate();
                
            }
    
           
            public override void OnApplyTemplate()
            {
                base.OnApplyTemplate();                       
                panel = GetTemplateChild("Panel") as Grid;
                                      
            }
    }

    Here is how the styling is done  for SampleControl in xaml

     

    <Style TargetType="src:SampleControl">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="src:SampleControl" >
                        <StackPanel x:Name="Container">
                            <Grid x:Name="Panel" HorizontalAlignment="Stretch" VerticalAlignment="Center">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition />
                                    <ColumnDefinition />
                                </Grid.ColumnDefinitions>
                               
                            </Grid>                    
                        </StackPanel>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

      In the Parent class, I create the object of Sample class and then perform some logic. Sadly this arrangement isn't working.

    On debugging, I found that onApplyTemplate of Sample class never gets called which is at the root of the problem.

    Can you please suggest as to what is it that I am doing wrong over here and how to correct the behaviour.

    Regards,

    Ffposts

     

    Thursday, February 18, 2010 4:18 AM

Answers

  • Ok, ApplyTemplate()  in ctor()  is a different discussion, Be sure, template will be applied without that

    Anyway ApplyTemplate() will not immediatelly applying templates, because it's depends from object rendering state.

    Seems to me your problem is - Sample control is not rendered to screen. I believ that you have created it from Parent class but without adding to child. (Children) .

    You can simple check ParentClass.Children.Add(new Sample Control()) and I believe that OnApplyTemplate will be called, otherwise you should check how and where you defined xaml for that

    Thursday, February 18, 2010 5:05 PM
  • When you create this SimpleControl from another UserControl, did you create it in code? If yes, did you put it into the VisualTree?

     

     

    Monday, February 22, 2010 3:33 PM

All replies

  • Hi,

    Try to use:

    this.DefaultStyleKey = typeof(SampleControl);

    instead:

    this.DefaultStyleKey = typeof(GroupCalendarPaneDefaultSetBarControl);

     

    Thursday, February 18, 2010 4:30 AM
  • Hi Sergey,

    I am using this.DefaultStyleKey = typeof(SampleControl);

    this.DefaultStyleKey = typeof(GroupCalendarPaneDefaultSetBarControl);  -- > was a typo mistake which I have now corrected in the original post.

    Regards,

    Ffposts

    Thursday, February 18, 2010 5:13 AM
  • Remove  this.ApplyTemplate() from ctor(), it not neccesary

     

     

    Thursday, February 18, 2010 6:33 AM
  • I removed and checked . still no solution

    this.ApplyTemplate() is used to make code more readable and as a way of ensuring that template is applied

    Thursday, February 18, 2010 7:32 AM
  • Ok, ApplyTemplate()  in ctor()  is a different discussion, Be sure, template will be applied without that

    Anyway ApplyTemplate() will not immediatelly applying templates, because it's depends from object rendering state.

    Seems to me your problem is - Sample control is not rendered to screen. I believ that you have created it from Parent class but without adding to child. (Children) .

    You can simple check ParentClass.Children.Add(new Sample Control()) and I believe that OnApplyTemplate will be called, otherwise you should check how and where you defined xaml for that

    Thursday, February 18, 2010 5:05 PM
  • @yurik72 - Can you elaborate on what you mean by your last sentence:

    You can simple check ParentClass.Children.Add(new Sample Control()) and I believe that OnApplyTemplate will be called, otherwise you should check how and where you defined xaml for that
     

    I'm confused by what you mean, there. If you mean to programmatically add an instance of SampleControl to the LayoutRoot of your application, then I understand you. I have tried this with no success.

    I am having the exact same problem as stated above. I have a control, it's default style is set in it's constructor, generic.xaml has very little if any XAML in it (to avoid confusion and other bugs), and generic.xaml is located in the /Themes directory of that project and it's Build Action is set to Resource.

    I have tried placing an instance of my custom control on the design surface in Blend as well as by programmatically adding an instance of the control to the LayoutRoot after the Loaded event of my application has fired. In both cases, OnApplyTemplate is never fired for my custom Control.

    What am I missing? Is there a project setting somewhere? Is there an assembly reference that I'm missing?

    Thanks,

    - Aaron

     

    Monday, February 22, 2010 3:24 PM
  • When you create this SimpleControl from another UserControl, did you create it in code? If yes, did you put it into the VisualTree?

     

     

    Monday, February 22, 2010 3:33 PM
  • The reason why OnApplyTemplate never fired, in my case, was because the XAML that comprised the control's default template in generic.xaml was corrupt. I had to remove every line of XAML and add it back to the project one by one to determine which controls broke the XAML parser.

    I have found this to be the case in nearly all the controls I've built where OnApplyTemplate does not fire. Check your XAML thoroughly and in the worst case, remove everything and add it all back one piece at a time.

    Monday, March 1, 2010 3:04 PM
  • You need to MergedDictionaries inside generic.xaml

    <ResourceDictionary
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
        
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="ms-appx:///AppStudio.Uwp/Controls/Carousel/Carousel.xaml"/>
            <ResourceDictionary Source="ms-appx:///AppStudio.Uwp/Controls/SplitterCard/SplitterCard.xaml"/>
            <ResourceDictionary Source="ms-appx:///AppStudio.Uwp/Controls/ErrorNotificationControl/ErrorNotificationControl.xaml"/>
            <ResourceDictionary Source="ms-appx:///AppStudio.Uwp/Controls/GifControl/GifControl.xaml"/>
            <ResourceDictionary Source="ms-appx:///AppStudio.Uwp/Controls/HtmlViewer/HtmlViewer.xaml"/>
            <ResourceDictionary Source="ms-appx:///AppStudio.Uwp/Controls/ImageEx/ImageEx.xaml"/>
            <ResourceDictionary Source="ms-appx:///AppStudio.Uwp/Controls/Indicator/Indicator.xaml"/>
            <ResourceDictionary Source="ms-appx:///AppStudio.Uwp/Controls/Pivorama/Pivorama.xaml"/>
            <ResourceDictionary Source="ms-appx:///AppStudio.Uwp/Controls/Relativebox/Relativebox.xaml"/>
            <ResourceDictionary Source="ms-appx:///AppStudio.Uwp/Controls/ResponsiveGridView/ResponsiveGridView.xaml"/>
            <ResourceDictionary Source="ms-appx:///AppStudio.Uwp/Controls/SearchBox/SearchBox.xaml"/>
            <ResourceDictionary Source="ms-appx:///AppStudio.Uwp/Controls/RoundButton/RoundButton.xaml"/>
            <ResourceDictionary Source="ms-appx:///AppStudio.Uwp/Controls/SectionList/SectionList.xaml"/>
            <ResourceDictionary Source="ms-appx:///AppStudio.Uwp/Controls/Virtualbox/Virtualbox.xaml"/>
            <ResourceDictionary Source="ms-appx:///AppStudio.Uwp/Controls/ShellControl/ShellControl.xaml"/>
            <ResourceDictionary Source="ms-appx:///AppStudio.Uwp/Controls/SliderView/SliderView.xaml"/>
            <ResourceDictionary Source="ms-appx:///AppStudio.Uwp/Controls/PropertySet/PropertySet.xaml"/>
            <ResourceDictionary Source="ms-appx:///AppStudio.Uwp/Controls/VariableSizedGrid/VariableSizedGrid.xaml"/>
            <ResourceDictionary Source="ms-appx:///AppStudio.Uwp/Controls/HtmlBlock/HtmlBlock.xaml"/>
    
            <ResourceDictionary Source="ms-appx:///AppStudio.Uwp/Labs/Accordion/Accordion.xaml"/>
            <ResourceDictionary Source="ms-appx:///AppStudio.Uwp/Labs/AutoHide/AutoHide.xaml"/>
            <ResourceDictionary Source="ms-appx:///AppStudio.Uwp/Labs/Mosaic/Mosaic.xaml"/>
            <ResourceDictionary Source="ms-appx:///AppStudio.Uwp/Labs/ResponsiveGridView/ResponsiveGridView.xaml"/>
            <ResourceDictionary Source="ms-appx:///AppStudio.Uwp/Labs/ResponsiveRow/ResponsiveRow.xaml"/>
            <ResourceDictionary Source="ms-appx:///AppStudio.Uwp/Labs/ResponsiveTextBlock/ResponsiveTextBlock.xaml"/>
            <ResourceDictionary Source="ms-appx:///AppStudio.Uwp/Labs/ShapeImage/ShapeImage.xaml"/>
            <ResourceDictionary Source="ms-appx:///AppStudio.Uwp/Labs/SlideShow/SlideShow.xaml"/>
        </ResourceDictionary.MergedDictionaries>
        
    </ResourceDictionary>


    amazestudio24


    • Edited by maze4 Monday, December 3, 2018 1:10 AM
    Monday, December 3, 2018 1:09 AM