Answered Access to Template Controls

  • 28. října 2006 20:50
     
     

    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.

Všechny reakce

  • 29. října 2006 2:45
     
     
    Page.Template.FindName() method can do the trick.

    Sheva
  • 29. října 2006 9:30
     
     

    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.

  • 29. října 2006 11:12
     
     
    Does the control template you create for the page really get applied to the Page?

    Sheva
  • 29. října 2006 11:35
     
     

    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

  • 29. října 2006 12:49
     
     Odpovědět
    Try to put the Page.Template.FindName() code into the Page.Loaded event handler.

    Sheva
  • 29. října 2006 12:56
     
     

    Hello,

    thank you. Now it works.

    Best regards,

    Thorsten