Does anyone know why OnApplyTemplate would not be called on a UserControl? I have been banging my head for several hours and its starting to hurt.
Real simply, I have a UserControl that is passed an object. It creates several UserControls programatically and adds them to a canvas. Those controls, in turn, create their own child UserControls and add them to an ItemsControl. So the hierarchy is:
UserControl A create UserControl B and adds them to child canvas
-- Canvas
---- UserControl B creates many UserControl Cs and adds them to the root UserControl C and as children of other UserControl Cs via the ItemsControl
------ Canvas
-------- Root UserControl C
---------- ItemsControl that contains more UserControl Cs
It's the UserControl C that is the problem. The template is loaded just fine, but OnApplyTemplate is never called, so my storyboards never load, which then means I can't show selection.
I put a breakpoint in there and it is never hit, though other breakpoints do get hit. Does anyone have any ideas?
Hello, normally UserControls don't have ControlTemplates. When you create a UserControl with XAML and code behind, you didn't create a ControlTemplate for the UserControl. So OnApplyTemplate is never called. The XAML of UserControl is loaded in the auto-generated
InitializeComponent method with this line:
System.Windows.
Application.LoadComponent(this,
new System.Uri("/YourAssembly;component/YourUserControl.xaml", System.UriKind.Relative));
You can begin your storyboards in the UserControl.Loaded event. Or you may go with the Custom Control route to use OnApplyTemplate. That is, don't use XAML and code behind. Create a class with pure code to define the Control's logic, and create a ControlTemplate
for this Control in the generic.xaml file.
shanaolanxing - I'll transfer to the Windows Azure team, and will have limited time to participate in the Silverlight forum. Apologize if I don't answer your questions in time.
One more possible problem of OnApplyTemplate not being called, could be if you are setting the visiblity of the class to Collapsed in the Constructor. This is especially for SL3.0. let me give you an example.
public class MyClass : ContentControl
{
public MyClass()
{
base.DefaultStyleKey = typeof(MyClass);
base.Visibility = Visibility.Collapsed; // Because of this line OnApplyTemplate will not invoke.
Minot
Member
17 Points
8 Posts
OnApplyTemplate not called
Apr 23, 2008 07:25 PM | LINK
Does anyone know why OnApplyTemplate would not be called on a UserControl? I have been banging my head for several hours and its starting to hurt.
Real simply, I have a UserControl that is passed an object. It creates several UserControls programatically and adds them to a canvas. Those controls, in turn, create their own child UserControls and add them to an ItemsControl. So the hierarchy is:
UserControl A create UserControl B and adds them to child canvas
-- Canvas
---- UserControl B creates many UserControl Cs and adds them to the root UserControl C and as children of other UserControl Cs via the ItemsControl
------ Canvas
-------- Root UserControl C
---------- ItemsControl that contains more UserControl Cs
It's the UserControl C that is the problem. The template is loaded just fine, but OnApplyTemplate is never called, so my storyboards never load, which then means I can't show selection.
I put a breakpoint in there and it is never hit, though other breakpoints do get hit. Does anyone have any ideas?
Yi-Lun Luo -...
All-Star
25149 Points
2759 Posts
Microsoft
Re: OnApplyTemplate not called
Apr 25, 2008 05:05 AM | LINK
Hello, normally UserControls don't have ControlTemplates. When you create a UserControl with XAML and code behind, you didn't create a ControlTemplate for the UserControl. So OnApplyTemplate is never called. The XAML of UserControl is loaded in the auto-generated InitializeComponent method with this line:
System.Windows.
Application.LoadComponent(this, new System.Uri("/YourAssembly;component/YourUserControl.xaml", System.UriKind.Relative));You can begin your storyboards in the UserControl.Loaded event. Or you may go with the Custom Control route to use OnApplyTemplate. That is, don't use XAML and code behind. Create a class with pure code to define the Control's logic, and create a ControlTemplate for this Control in the generic.xaml file.
DotNet_Expert
Member
48 Points
55 Posts
Re: OnApplyTemplate not called
Nov 03, 2009 08:07 AM | LINK
One more possible problem of OnApplyTemplate not being called, could be if you are setting the visiblity of the class to Collapsed in the Constructor. This is especially for SL3.0. let me give you an example.
public class MyClass : ContentControl
{
public MyClass()
{
base.DefaultStyleKey = typeof(MyClass);
base.Visibility = Visibility.Collapsed; // Because of this line OnApplyTemplate will not invoke.
}
}