访问ControlTemplate内部的网格

时间:2018-12-12 23:17:12

标签: c# wpf xaml controltemplate

我有带有网格的ControlTemplate。但是我找不到在包含我的模板的控件中访问此网格的任何方法:(

<Window.Resources>
    <ControlTemplate TargetType="ContentControl" x:Key="CertificatesTable">
        <Grid Margin="5">
            ...
        </Grid>
    </ControlTemplate>
</Window.Resources>
...
<Border Grid.Row="2" Grid.ColumnSpan="100" BorderThickness="1,0,1,1" BorderBrush="#99000000">
    <ContentControl Name="Certificates1" Template="{StaticResource CertificatesTable}"/>
</Border>
<Border Grid.Row="4" Grid.ColumnSpan="100" BorderThickness="1,0,1,1" BorderBrush="#99000000">
    <ContentControl Name="Certificates2" Template="{StaticResource CertificatesTable}"/>
</Border>
<Border Grid.Row="6" Grid.ColumnSpan="100" BorderThickness="1,0,1,1" BorderBrush="#99000000">
    <ContentControl Name="Certificates3" Template="{StaticResource CertificatesTable}"/>
</Border>
...

1 个答案:

答案 0 :(得分:0)

欢迎波马到堆栈溢出。

这是简单的代码,如何在代码端访问或获取网格详细信息。

<Window.Resources>
    <ControlTemplate x:Name="myControl" TargetType="ContentControl" x:Key="CertificatesTable">
        <Grid Margin="5" x:Name="grv">
            ...
        </Grid>
    </ControlTemplate>
</Window.Resources>

C#代码以访问网格详细信息-

ar template = myControl.Template;
var myControl = (Grid)template.FindName("grv", myControl);