gpt4 book ai didi

c# - WPF Datagrid 获取所有选中的行(不使用 WPF 绑定(bind))

转载 作者:行者123 更新时间:2023-11-30 22:03:58 26 4
gpt4 key购买 nike

如何使用后面的 C# 代码获取 WPF 数据网格中所有选中的行?

我正在尝试使用这些代码进行批量删除,但我需要先获取所有选中的行。

这是我的数据网格

<DataGrid x:Name="DiseaseSymptomsDataGrid" Grid.Row="1"
SelectionMode="Extended"
GridLinesVisibility="All"
CanUserSortColumns="False"
CanUserAddRows="False"
HeadersVisibility="Column"
AutoGenerateColumns="False"
PreviewMouseLeftButtonUp="DiseaseSymptomsDataGrid_OnPreviewMouseLeftButtonUp"
>

<DataGrid.Columns>
<DataGridTextColumn x:Name="IdColumn" Binding="{Binding Id}" Header="Id" Visibility="Collapsed" IsReadOnly="True"/>
<DataGridTextColumn x:Name="DiseaseIdColumn" Binding="{Binding DiseaseId}" Header="DiseaseId" Visibility="Collapsed" IsReadOnly="True"/>
<DataGridTextColumn x:Name="DiseaseNameColumn" Binding="{Binding DiseaseName}" Header="Disease" Visibility="Collapsed" IsReadOnly="True"/>
<DataGridTextColumn x:Name="SymptomIdColumn" Binding="{Binding SymptomId}" Header="SymptomId" Visibility="Collapsed" IsReadOnly="True"/>
<DataGridTextColumn x:Name="SymptomNameColumn" Binding="{Binding SymptomName}" Header="Symptom" IsReadOnly="True"/>
<DataGridTextColumn x:Name="DescriptionColumn" Binding="{Binding SymptomDescription}" Header="Description" Width="425" IsReadOnly="True"/>
<DataGridCheckBoxColumn x:Name="StatusIdColumn" Header="Delete" IsReadOnly="False"/>
</DataGrid.Columns>

</DataGrid>

下面是我尝试的代码

public List<DiseaseSymptomParams> GetSelectedDiseaseSymptom()
{

var entiParams = new DiseaseSymptomParams();
var selectedDiseases = new List<DiseaseSymptomParams>();

try
{
// this will only get the highlighted row, not ALL the checked rows
foreach (DiseaseSymptom itemSelected in DiseaseSymptomsDataGrid.SelectedItems)
{
entiParams.Id = DefaultValue.GetInt(itemSelected.Id);

selectedDiseases.Add(entiParams);

}

}
catch (Exception)
{

}
return selectedDiseases;

}

最佳答案

这是你可以做的:

foreach (DiseaseSymptom item in DiseaseSymptomsDataGrid.ItemsSource)
{
if (((CheckBox)StatusIdColumn.GetCellContent(item)).IsChecked == true)
{
selectedDiseases.Add(item);
}
}

不符合 MVVM 但务实。

关于c# - WPF Datagrid 获取所有选中的行(不使用 WPF 绑定(bind)),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25596187/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com