我正在尝试访问代码中的DataGrid列标题中的复选框 但Template.FindName始终返回null。谁能告诉我这里我做错了什么?
这是我的代码背后:
private void MyCheckBox_Checked(object sender, RoutedEventArgs e)
{
var template = MyGrid.Template;
var myCheckbox = (CheckBox)template.FindName("MyChkBxAll", this.MyGrid);
}
XAML:
<Grid>
<DataGrid Name="MyGrid" Margin="0,10,0,0" HeadersVisibility="Column" VerticalAlignment="Top" FontSize="14" Width="280" Height="220" MaxWidth="700" MinWidth="200" Background="#FFF3F3F3" RowBackground="#FF37AEC9" AlternatingRowBackground="LightBlue" HorizontalAlignment="Center" CanUserAddRows="False" KeyboardNavigation.TabNavigation="None" AutoGenerateColumns="True" GridLinesVisibility="None" IsReadOnly="True" VirtualizingStackPanel.VirtualizationMode="Standard" ColumnWidth="*" HorizontalScrollBarVisibility="Auto" SelectionChanged="InstallableGrid_SelectionChanged">
<DataGrid.Columns>
<DataGridCheckBoxColumn Header="Select" ElementStyle="{StaticResource CheckBoxStyleInverted}" Width="Auto" >
<DataGridCheckBoxColumn.HeaderTemplate >
<DataTemplate x:Name="MyChkBx">
<CheckBox x:Name="MyChkBxAll" Style="{StaticResource CheckBoxStyleInverted}" Margin="-3.2,0,0,0" HorizontalAlignment="Left" Checked="MyCheckBox_Checked" Width="50" Height="30"/>
</DataTemplate>
</DataGridCheckBoxColumn.HeaderTemplate>
</DataGridCheckBoxColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>