C# 为自动生成的列datagrid中的每个复选框注册选中的事件处理程序
我的最终目标是用自动生成的列构建一个datagrid(因为我永远不知道需要生成多少类属性)。此datagrid将始终将第一列作为复选框。如果用户在datagrid中选择多行,然后选中其中一个选中的复选框,则所有复选框(在第一列或相关列中-两者都可以)也将被选中 我曾在xaml中尝试使用DataGrid.RowHeaderTemplate,如下所示:C# 为自动生成的列datagrid中的每个复选框注册选中的事件处理程序,c#,wpf,datagrid,event-handling,C#,Wpf,Datagrid,Event Handling,我的最终目标是用自动生成的列构建一个datagrid(因为我永远不知道需要生成多少类属性)。此datagrid将始终将第一列作为复选框。如果用户在datagrid中选择多行,然后选中其中一个选中的复选框,则所有复选框(在第一列或相关列中-两者都可以)也将被选中 我曾在xaml中尝试使用DataGrid.RowHeaderTemplate,如下所示: <DataGrid x:Name="SheetListGrid" HorizontalAlignment
<DataGrid x:Name="SheetListGrid"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
HeadersVisibility="All"
AutoGenerateColumns="True"
CanUserResizeRows="False"
AlternatingRowBackground="LightGray"
AutoGeneratingColumn="SheetListGrid_AutoGeneratingColumn">
<DataGrid.RowHeaderTemplate>
<DataTemplate>
<Grid>
<CheckBox Checked="CheckBox_Checked"
IsChecked="{Binding Path=Selected, Mode=TwoWay}"/>
</Grid>
</DataTemplate>
</DataGrid.RowHeaderTemplate>
</DataGrid>
这是我的复选框\u Checked事件处理程序,以防有用
private void CheckBox_Checked(object sender, RoutedEventArgs e)
{
// get the selected sheets
var selectedSheets = SheetListGrid.SelectedItems;
if (selectedSheets.Count == 0) return;
foreach (var item in selectedSheets)
{
//DataGridRow row = SheetListGrid.ItemContainerGenerator.ContainerFromItem(item) as DataGridRow;
FakeSheet sheet = item as FakeSheet;
if (sheet.Selected == false)
sheet.Selected = true;
}
SheetListGrid.Items.Refresh();
}
我觉得好像我错过了一些东西,使这一切变得容易了很多,但我无法找出它,我几乎融化了我打开的标签数量的内存。我应该注意,这个项目将是一个类库,我不能包括任何第三方程序集。另外,我希望尽量避免使用Windows.Interactivity,因为我以前在将这个类库分发给其他人时遇到过问题,并且有一台计算机运行一个单独的版本导致了错误 这就是你需要的吗?我不这么认为。这个所谓的链接所讨论的似乎是一个“全选”功能。我正在寻找更多的“只检查选择了行的那些”。
private void CheckBox_Checked(object sender, RoutedEventArgs e)
{
// get the selected sheets
var selectedSheets = SheetListGrid.SelectedItems;
if (selectedSheets.Count == 0) return;
foreach (var item in selectedSheets)
{
//DataGridRow row = SheetListGrid.ItemContainerGenerator.ContainerFromItem(item) as DataGridRow;
FakeSheet sheet = item as FakeSheet;
if (sheet.Selected == false)
sheet.Selected = true;
}
SheetListGrid.Items.Refresh();
}