我有一个 ContentControl,它的模板会根据 DataContext 中的某个值进行更改。
<ContentControl x:Name="params_control">
<ContentControl.Style>
<Style TargetType="ContentControl">
<Style.Triggers>
<DataTrigger Binding="{Binding Type}" Value="Type1">
<Setter Property="Template" Value="{StaticResource template1}" />
</DataTrigger>
<DataTrigger Binding="{Binding Type}" Value="Type2">
<Setter Property="Template" Value="{StaticResource template2}" />
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>
模板是一个带有许多 TextBlock 和 TextBox 的网格,如下所示:
<ControlTemplate x:Key="template1" TargetType="{x:Type ContentControl}">
<Grid>
<TextBlock Text="Period"/>
<TextBox Text="{Binding Period}"/>
</Grid>
</ControlTemplate>
<ControlTemplate x:Key="template2" TargetType="{x:Type ContentControl}">
<Grid>
<TextBlock Text="Period2"/>
<TextBox Text="{Binding Period2}"/>
</Grid>
</ControlTemplate>
模板切换和数据绑定效果很好。
问题是我需要遍历当前加载的模板中的输入字段以检查验证错误,但调用LogicalTreeHelper.GetChildren(params_control)
什么也不返回。
为什么 LogicalTreeHelper 看不到模板化的父子节点?