Skip to main content

Microsoft Silverlight

Answered Question Custom Control, Initialization Order?RSS Feed

(0)

codebased
codebased

Participant

Participant

806 points

360 Posts

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 then please mark it as "Answered".

Yi-Lun Luo - MSFT
Yi-Lun L...

All-Star

All-Star

25052 points

2,747 Posts

Answered Question

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.

codebased
codebased

Participant

Participant

806 points

360 Posts

Re: Custom Control, Initialization Order?

I did the same and it works. Thanks a ton.

"If I've answered your query then please mark it as "Answered".

coughlinj
coughlinj

Member

Member

334 points

114 Posts

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

R3al1ty
R3al1ty

Member

Member

185 points

72 Posts

Re: Re: Custom Control, Initialization Order?

I second this, can someone blog about this topic?

Dave Relyea
Dave Relyea

Participant

Participant

1084 points

249 Posts

Microsoft

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.

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities