我正在尝试使用 Template.FindName("Name",templatedParent) 函数从模板中访问控件。由于某种原因,它返回 null。我正在使用 XamlReader 加载以下 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>
在上面的 xaml 中,我有一个名为 ApplicationContainer 的自定义控件。我已经在代表类的代码中覆盖了 OnApplyTemplate() 方法,并将上述样式应用于控件(尚未定义 x:Key,因此默认情况下会应用它)。我在 XamlReader.Load() 上看到 OnApplyTemplate() 没有被调用。除此之外,如果我尝试访问 _ae51329a9692472099e1c0ed6b6b7937.Template 我可以看到模板中定义的所有内容(所以,我觉得模板应用正确)
但是当我在控制类 OnApplyTemplate 方法中执行 this.Template.FindName("PART_HostArea",this) 时(我明确地调用它)它返回我为空。