【问题标题】:Accessing Grid inside ControlTemplate访问 ControlTemplate 中的 Grid
【发布时间】:2018-12-12 23:17:11
【问题描述】:

我有 ControlTemplate 和 Grid 里面。但是我找不到任何方法可以在包含我的模板的控件中访问此 Grid :(

<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>
...

【问题讨论】:

  • “访问”是什么意思。你想完成什么?

标签: c# wpf xaml controltemplate


【解决方案1】:

Pomah 欢迎来到 stack-overflow。

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

<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);

【讨论】:

  • 非常感谢!它可以工作,但我必须为我的控件调用 ApplyTemplate(),因为 FindName() 返回 null。
猜你喜欢
  • 2013-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多