Page view counter
Custom Control, Initialization Order?
Last post 10-11-2008 1:29 PM by Dave Relyea. 5 replies.
Sort Posts:
10-06-2008 8:41 PM
Custom Control, Initialization Order?
 
 I'm creating a custom control class inherited from the ContentControl.
 
[TemplatePart(Name = PagingBase.NavigationBarTopPanelName, Type = typeof(StackPanel))]
[TemplatePart(Name = PagingBase.NavigationBarBottomPanelName, Type = typeof(StackPanel))]
 public class
PagingBase: ContentControl { ... ... }
The template is as follow:
<Style TargetType="local:PagingBase"> <Setter Property="Background"> <Setter.Value> <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> <GradientStop Color="#FFFFFCF4" Offset="0"/> <GradientStop Color="#FFDAD8C9" Offset="1"/> </LinearGradientBrush> </Setter.Value> </Setter> <Setter Property="Foreground" Value="#FF000000"/> <Setter Property="BorderThickness" Value="2,0,2,2"/> <Setter Property="BorderBrush" Value="#FF2366C6"/> <Setter Property="OpaqueColor" Value="#44FFFFFF"/> <Setter Property="NavigationBarBackground"> <Setter.Value> <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> <GradientStop Color="#FFA9CCFF" Offset="0.00" /> <GradientStop Color="#FF8EBBFC" Offset="0.28" /> <GradientStop Color="#FF68A3F8" Offset="0.32" /> <GradientStop Color="#FF458DF5" Offset="0.90" /> <GradientStop Color="#FF2366C6" Offset="1.00" /> </LinearGradientBrush> </Setter.Value> </Setter> <Setter Property="NavigationBarForeground" Value="#FFFFFFFF" /> <Setter Property="Padding" Value="10"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="local:PagingBase"> <Grid x:Name="GridRoot" Background="{TemplateBinding OpaqueColor}" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="30"/> <RowDefinition Height="*"/> <RowDefinition Height="30"/> </Grid.RowDefinitions> <!-- Top Navigation Bar --> <StackPanel Grid.Row="0" Orientation="Horizontal" x:Name="NavigationBarTopPanel" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="5" /> <!-- Content presenter--> <ContentPresenter Grid.Row="1" x:Name="contentPresenter" Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}"/> <!-- Bottom Navigation Bar --> <StackPanel Grid.Row="2" Orientation="Horizontal" x:Name="NavigationBarBottomPanel" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="5" /> </Grid> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style>


The way I am using in the User Control is:

<self:PagingBase x:Name="EmployeeListPage" TotalRecords="100" PageSize="5" OnPageChanged="EmployeeList_OnPageChanged" Orientation="Both" >

...
</...>
I'm expecting two controls to be accessible before the value is being set through Orientation property however, they are not avaiable when the object is constructed and I can only access this once OnApplyTemplate is being called.
 
I'm setting the default style key = typeof(PagingBase) in the constructor of this control.
 
Any idea how the initialisation order work for the custom controls?
 

"If I've answered your query, please mark "Answered".

codebased

Loading...
Joined on 03-05-2008
Australia
Posts 183
10-08-2008 6:05 AM
Marked as Answer
Re: Custom Control, Initialization Order?

Hello, can you be more specific on your requirement? Do you want to access EmployeeListPage.Orientation in the UserControl's constructor? That will not work. You need to call EmployeeListPage.ApplyTemplate to force the template to be applied.

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.

Yi-Lun Luo - MSFT

Loading...
Joined on 10-29-2007
Posts 2,688
10-08-2008 6:28 PM
Re: Custom Control, Initialization Order?
I did the same and it works. Thanks a ton.

"If I've answered your query, please mark "Answered".

codebased

Loading...
Joined on 03-05-2008
Australia
Posts 183
10-08-2008 8:23 PM
Re: Custom Control, Initialization Order?

 I'm constantly questioning myself as to when the ApplyTemplate is called in the lifecycle.  I have even noticed and have been shocked that it can be called multiple times... usually when this happens its because I've updated the ContentTemplate or something (sorry don't remember the extact instance).

 Anyways I'm not sure if this was the original question but I would like to see a writeup on the LifeCycle of a Custom Control.  The key items I'm looking for are when are the following called and in what order...

OnApplyTemplate,

Measure

Arrange

coughlinj

Loading...
Joined on 03-07-2008
Vancouver
Posts 114
10-08-2008 10:31 PM
Re: Re: Custom Control, Initialization Order?

I second this, can someone blog about this topic?

R3al1ty

Loading...
Joined on 05-03-2008
Posts 68
10-11-2008 1:29 PM
Re: Re: Custom Control, Initialization Order?

http://blogs.msdn.com/devdave/archive/2008/10/11/control-lifecycle.aspx

Hope that helps a bit. If there are other things you'd like to see in the table, please let me know.

Dave Relyea [MSFT]
http://blogs.msdn.com/devdave

Dave Relyea

Loading...
Joined on 05-08-2007
Posts 245
Microsoft Communities