Access to Template Controls

Hello,

here some xaml:

<ResourceDictionary

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

xmlns:c="clr-namespace:myTest">

<ResourceDictionary.MergedDictionaries>

<ResourceDictionary Source="Common.xaml" />

</ResourceDictionary.MergedDictionaries>

<Style x:Key="DefaultLayout" TargetType="{x:Type Page}">

<Style.Resources>

<ObjectDataProvider x:Key="LayoutData" ObjectType="{x:Type c:DefaultLayoutData}" />

<ScaleTransform x:Key="ScaleTransform" ScaleX="1" ScaleY="1" />

</Style.Resources>

<Setter Property="Template">

<Setter.Value>

<ControlTemplate TargetType="{x:Type Page}">

<Grid Name="backGrid" Background="{StaticResource PageBackgroundBrush}" LayoutTransform="{StaticResource ScaleTransform}">

<Grid.ColumnDefinitions>

<ColumnDefinition Width="*" />

<ColumnDefinition Width="3*" />

</Grid.ColumnDefinitions>

<Grid.RowDefinitions>

<RowDefinition Height="100" />

<RowDefinition />

</Grid.RowDefinitions>

<Label Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="2" Content="{TemplateBinding Property=Page.Title}"></Label>

<Label Name="lblTest" Grid.Column="1" Grid.Row="0">

<Binding Source="{StaticResource LayoutData}" Mode="OneTime" Path="PageSubTitle"/>

</Label>

<!--More Controls-->

</Grid>

</ControlTemplate>

</Setter.Value>

</Setter>

</Style>

</ResourceDictionary>

How can i access the frm Template generatet Controls in the Page Code behind

Thorsten

Sorry for my bad english.




Answer this question

Access to Template Controls

  • joe.hamilton

    Page.Template.FindName() method can do the trick.

    Sheva


  • Tarey Wolf

    Try to put the Page.Template.FindName() code into the Page.Loaded event handler.

    Sheva


  • D11

    Does the control template you create for the page really get applied to the Page

    Sheva


  • Phill Midwinter

    Hello,

    thank you. Now it works.

    Best regards,

    Thorsten



  • marcy

    Hello,

    here my code in page:

    <Page x:Class="Page2"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Title="bilder" Style="{StaticResource DefaultLayout}" ShowsNavigationUI="False"

    >

    </Page>

     

    Code Behind:

    Partial Public Class Page2

    Inherits System.Windows.Controls.Page

    Dim LayoutData As DefaultLayoutData

    Public Sub New()

    InitializeComponent()

    Dim o As ObjectDataProvider

    o = Me.Style.Resources("LayoutData")

    LayoutData = o.Data

    LayoutData.CurrentDescription = "current description"

    LayoutData.CurrentItem = "currentitem"

    LayoutData.PageSubTitle = "subtitle"

     

    Dim obj As Object = Template.FindName("backGrid", Me)

       If obj Is Nothing Then

          Console.WriteLine("null")

       Else

           Console.WriteLine(obj.ToString)

       End If

    End Sub

    End Class

    Thats all. I can see all controls where i defined in the Style "DefaultLayout".

    I hope you mean this...my english...

    Thorsten



  • djchapin

    Hello,

    i`ve tried this:

    Dim obj As Object = Template.FindName("backGrid", Me)

       If obj Is Nothing Then

          Console.WriteLine("null")

       Else

           Console.WriteLine(obj.ToString)

       End If

     

    Thats don't work. The Function returns nothing.



  • Access to Template Controls