Anee
Anee

Reputation: 483

Not able to find a control within a ControlTemplate

I am trying to access a control from within a template using the Template.FindName("Name",templatedParent) function. For some reason it is returning null. I am using the XamlReader to load the below xaml.

<Window x:Class="WpfApplication2.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication2;assembly=WpfApplication2"
    xmlns:ice="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options"
    xmlns:igecac="clr-namespace:namespace1;assembly=assembly1"
    Loaded="Window_Loaded"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <Style TargetType="{x:Type igecac:ApplicationContainer}">
        <Setter Property="Control.Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type igecac:ApplicationContainer}">
                    <ControlTemplate.Resources>
                        <DataTemplate x:Key="selectedItemTemplate">
                            <TextBlock Padding="10,10,10,10" Text="{Binding Path=Id}" Background="{DynamicResource PanelBrush}" Width="100" Height="50">
              <TextBlock.Foreground>
                <SolidColorBrush Color="Black" ice:Freeze="True" />
              </TextBlock.Foreground>
                            </TextBlock>
                        </DataTemplate>
                        <DataTemplate x:Key="ItemTemplate">
                            <TextBlock Padding="10,10,10,10" Text="{Binding Path=Id}" Background="{DynamicResource ObjectStrokeBrush}" Width="100" Height="50">
              <TextBlock.Foreground>
                <SolidColorBrush Color="Black" ice:Freeze="True" />
              </TextBlock.Foreground>
                            </TextBlock>
                        </DataTemplate>
                    </ControlTemplate.Resources>
                    <Grid Background="{DynamicResource ObjectGradientBrush}">
                        <Grid.RowDefinitions />
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Name="grd1" Width="110" />
                            <ColumnDefinition Width="50" />
                            <ColumnDefinition Width="2*" />
                        </Grid.ColumnDefinitions>
                        <Canvas Margin="0,-7,0,0" Width="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=HostableWidth}" Height="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=HostableHeight}">
                            <ContentPresenter Name="PART_HostArea" Content="{Binding Path=SelectedPanel.Document.Editor}" />
                        </Canvas>
                        <igecac:CustomListBox Name="PART_AssociatedPanels" ItemsSource="{Binding Path=AssociatedPanels}" SelectedItem="{Binding Path=SelectedPanel, Mode=TwoWay}" IsSynchronizedWithCurrentItem="True" HorizontalContentAlignment="Stretch" Background="{DynamicResource ObjectGradientBrush}">
                            <igecac:CustomListBox.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <StackPanel Orientation="Vertical" IsItemsHost="True" />
                                </ItemsPanelTemplate>
                            </igecac:CustomListBox.ItemsPanel>
                            <igecac:CustomListBox.ItemContainerStyle>
                                <Style TargetType="{x:Type ListBoxItem}">
                                    <Setter Property="ContentControl.ContentTemplate" Value="{StaticResource ItemTemplate}" />
                                    <Setter Property="Control.HorizontalContentAlignment" Value="Stretch" />
                                    <Setter Property="FrameworkElement.Margin" Value="1,1,1,1" />
                                    <Setter Property="Control.Padding" Value="2,2,2,2" />
                                    <Setter Property="UIElement.Visibility" Value="Visible" />
                                    <Style.Triggers>
                                        <Trigger Property="Selector.IsSelected" Value="True">
                                            <Setter Property="ContentControl.ContentTemplate" Value="{StaticResource selectedItemTemplate}" />
                                        </Trigger>
                                    </Style.Triggers>
                                </Style>
                            </igecac:CustomListBox.ItemContainerStyle>
                        </igecac:CustomListBox>
                        <Grid Name="CloseButton" Margin="-6,-6,0,0" Grid.Column="1" Width="50" Height="40" HorizontalAlignment="Left" Visibility="Visible" VerticalAlignment="Top">
                            <Grid.RowDefinitions />
                            <Grid.ColumnDefinitions />
                            <Rectangle Fill="{DynamicResource ObjectStrokeBrush}" Width="40" Height="25" HorizontalAlignment="Center" VerticalAlignment="Center" />
                            <Label Content="Close" HorizontalAlignment="Center" VerticalAlignment="Center" />
                        </Grid>
                        <Grid Name="OpenButton" Margin="-5,-6,0,0" Grid.Column="0" Width="50" Height="40" HorizontalAlignment="Left" Visibility="Hidden" VerticalAlignment="Top">
                            <Grid.RowDefinitions />
                            <Grid.ColumnDefinitions />
                            <Rectangle Fill="{DynamicResource ObjectStrokeBrush}" Width="40" Height="25" HorizontalAlignment="Center" VerticalAlignment="Center" />
                            <Label Content="Open" HorizontalAlignment="Center" VerticalAlignment="Center" />
                        </Grid>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <EventTrigger SourceName="CloseButton" RoutedEvent="Mouse.MouseDown">
                            <EventTrigger.Actions>
                                <BeginStoryboard>
                                    <BeginStoryboard.Storyboard>
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="PART_AssociatedPanels" Storyboard.TargetProperty="(UIElement.Visibility)">
                                                <DiscreteObjectKeyFrame KeyTime="00:00:00" Value="{x:Static Visibility.Hidden}" />
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="OpenButton" Storyboard.TargetProperty="(UIElement.Visibility)">
                                                <DiscreteObjectKeyFrame KeyTime="00:00:00" Value="{x:Static Visibility.Visible}" />
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="CloseButton" Storyboard.TargetProperty="(UIElement.Visibility)">
                                                <DiscreteObjectKeyFrame KeyTime="00:00:00" Value="{x:Static Visibility.Hidden}" />
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </BeginStoryboard.Storyboard>
                                </BeginStoryboard>
                            </EventTrigger.Actions>
                        </EventTrigger>
                        <EventTrigger SourceName="OpenButton" RoutedEvent="Mouse.MouseDown">
                            <EventTrigger.Actions>
                                <BeginStoryboard>
                                    <BeginStoryboard.Storyboard>
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="PART_AssociatedPanels" Storyboard.TargetProperty="(UIElement.Visibility)">
                                                <DiscreteObjectKeyFrame KeyTime="00:00:00" Value="{x:Static Visibility.Visible}" />
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="OpenButton" Storyboard.TargetProperty="(UIElement.Visibility)">
                                                <DiscreteObjectKeyFrame KeyTime="00:00:00" Value="{x:Static Visibility.Hidden}" />
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="CloseButton" Storyboard.TargetProperty="(UIElement.Visibility)">
                                                <DiscreteObjectKeyFrame KeyTime="00:00:00" Value="{x:Static Visibility.Visible}" />
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </BeginStoryboard.Storyboard>
                                </BeginStoryboard>
                            </EventTrigger.Actions>
                        </EventTrigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="igecac:ApplicationContainer.HostableWidth" Value="720" />
        <Setter Property="igecac:ApplicationContainer.HostableHeight" Value="590" />
    </Style>
</Window.Resources>
<Grid>
    <igecac:ApplicationContainer x:Name="_ae51329a9692472099e1c0ed6b6b7937"
                                     Tag="TemplatePart" Width="800" Height="600"
                                     HostableWidth="500" HostableHeight="500">
    </igecac:ApplicationContainer>
    <igecac:DecimalKeypad/>
    <igecac:HexKeypad/>
</Grid>

In the above xaml i have a custom control called ApplicationContainer. I have overridden the OnApplyTemplate() method in the code representing the class and i apply the above style to the control(haven't defined x:Key so it gets applied by default). I see that on XamlReader.Load() the OnApplyTemplate() not called. That apart, if i try accessing the _ae51329a9692472099e1c0ed6b6b7937.Template i can see everything defined in the template(So, i feel the template is applied properly)

But when i do this.Template.FindName("PART_HostArea",this) in the control class OnApplyTemplate method(i call it explicitly) it returns me null.

Upvotes: 0

Views: 210

Answers (1)

user786981
user786981

Reputation:

Have you tried GetTemplateChild(ControlName)?

Upvotes: 1

Related Questions