我有一个WPF DataGrid控件,我还有一个DataTemplate(CheckBox)控件,如下所示
<DataGridTemplateColumn Width="50" MinWidth="20" >
<DataGridTemplateColumn.HeaderTemplate>
<DataTemplate>
<!--<TextBlock Text="Manage" FontSize="18" Foreground="#FF666666" HorizontalAlignment="Center" Margin="50,0" />-->
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" Margin="10,0,0,0" >
<CheckBox x:Name="chkBox_CheckAllStuRow" Width="20" Height="auto" Checked="chkBox_CheckAllStuRow_Checked" />
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.HeaderTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" >
<CheckBox x:Name="chkBox_CheckStuRow" Width="20" Height="20" />
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="First Name" Width="*" MinWidth="100" Binding="{Binding f_name}" />
<DataGridTextColumn Header="Last Name" Width="*" MinWidth="100" Binding="{Binding l_name}" />
<DataGridTextColumn Header="Phone No" Width="*" MinWidth="100" Binding="{Binding phone}" />
<DataGridTemplateColumn Width="auto" MinWidth="250" >
<DataGridTemplateColumn.HeaderTemplate>
<DataTemplate>
<TextBlock Text="Manage" FontSize="18" Foreground="#FF666666" HorizontalAlignment="Center" Margin="50,0" />
</DataTemplate>
</DataGridTemplateColumn.HeaderTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" >
<Button x:Name="btn_ViewStu" Content="View" FontSize="14" HorizontalAlignment="Center" Height="27" Margin="3" Style="{DynamicResource ActiveButtonStyle}" Width="65" Click="btn_ViewStu_Click" />
<Button x:Name="btn_DeleteStu" Content="Delete" FontSize="14" HorizontalAlignment="Center" Height="27" Margin="3" Style="{DynamicResource DangerButtonStyle}" Width="65" Click="btn_DeleteStu_Click" />
<Button x:Name="btn_withDrwStu" Content="Withdraw" FontSize="14" HorizontalAlignment="Center" Height="27" Margin="3" Style="{DynamicResource DangerButtonStyle}" Width="70" />
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
我现在想要实现的是从我的C#代码访问复选框控件的变量名。提前谢谢。
答案 0 :(得分:0)
你可以试试这个
List<string> chBoxName = new List<string>();
foreach (CheckBox tb in FindVisualChildren<CheckBox>(Window))
{
chBoxName.Add(tb.Name);
}
chBoxName 属性包含List<string>
类型的所有复选框名称。
助手方法
public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
{
if (depObj != null)
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
if (child != null && child is T)
{
yield return (T)child;
}
foreach (T childOfChild in FindVisualChildren<T>(child))
{
yield return childOfChild;
}
}
}
}