0
Template.FindName ("Name", templatedParent) 함수를 사용하여 템플릿 내에서 컨트롤에 액세스하려고합니다. 어떤 이유로 그것은 null을 반환합니다. XamlReader를 사용하여 아래 xaml을로드하고 있습니다. 상기 XAML에서 ControlTemplate 내에서 컨트롤을 찾을 수 없습니다.
<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>
난 ApplicationContainer라는 맞춤 제어가있다. 클래스를 나타내는 코드에서 OnApplyTemplate() 메서드를 재정의하고 위 스타일을 컨트롤에 적용합니다 (x : Key를 정의하지 않아 기본적으로 적용됩니다). XamlReader.Load()에서 OnApplyTemplate()이 호출되지 않은 것을 볼 수 있습니다. 그 떨어져, _ae51329a9692472099e1c0ed6b6b7937.Template에 액세스하려고하면 템플릿에 정의 된 모든 것을 볼 수 있습니다. (템플릿이 올바르게 적용된 것 같아요.)
하지만이 템플릿을 만들 때 .Template.FindName ("PART_HostArea", this) OnApplyTemplate 컨트롤 클래스 (명시 적으로 호출)에서 null을 반환합니다.
글쎄, 나는 대답을 스스로 발견했다. 나는 그것을 작동시키기 위해 Show() 윈도우를 가져야 만했다. – Anee