我有超过1000条数据要在WPF的DataGrid中显示,并且我在DataGrid中添加了一个复选框。选中多个复选框时,可以将选中的索引添加到指定的索引集中。代码如下:
XAML:
<DataGrid x:Name="textWmdg" AutoGenerateColumns="False" Grid.Row="0" VerticalAlignment="Top" ItemsSource="{Binding waterTexts}" Height="252" **EnableRowVirtualization="True"**
CanUserSortColumns="False" Margin="2,2,2,2"
VirtualizingPanel.VirtualizationMode="Standard"
CanUserResizeColumns="False" CanUserResizeRows="False" RowHeaderWidth="0"
CanUserReorderColumns="False" CanUserAddRows="False" Style="{StaticResource DataGridStyle}"VirtualizingPanel.IsVirtualizing="True" DataGrid.RowHeight="22" RowDetailsVisibilityMode="Visible" LoadingRow="TextWmdg_LoadingRow">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Select" Width="*" IsReadOnly="False">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" >
<CheckBox x:Name="textSelect" Click="TextSelected_Click"/>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="NO." Width="*" Binding="{Binding Header, RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}, Mode=FindAncestor}}" IsReadOnly="True"/>
<DataGridTemplateColumn Header="Color" Width="3*" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" >
<Label Width="15" BorderThickness="0.5" BorderBrush="Black" Background="{Binding TextColor}"/>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="Sign" Width="3*" Binding="{Binding Sign}" IsReadOnly="True"/>
<DataGridTemplateColumn Header="KeyWord" Width="12*" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding ShowText}" TextAlignment="Left" HorizontalAlignment="Left">
<TextBlock.ToolTip>
<TextBlock Text="{Binding ShowText}" TextWrapping="Wrap"/>
</TextBlock.ToolTip>
</TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
CS:
private void TextSelected_Click(object sender, RoutedEventArgs e)
{
deltextindex.Clear();
for (int i = 0; i < textWmdg.Items.Count; i++)
{
DataGridTemplateColumn selectcolumn = textWmdg.Columns[0] as DataGridTemplateColumn;
FrameworkElement selectment = selectcolumn.GetCellContent(textWmdg.Items[i]);
CheckBox textcheckbox = selectcolumn.CellTemplate.FindName("textSelect", selectment) as CheckBox;
if (textcheckbox.IsChecked == true)
{
deltextindex.Add(i);
}
}
}
之前没有问题,只是显示速度很慢。今天,为了追求更快的速度,我将DataGrid的EnableRowVirtualization设置为true结果,我无法正常打开复选框。建议System.ArgumentNullException。如果我可以提高显示速度并正确获得复选框的索引呢?谢谢你!
如果您在视图模型上创建属性IsTextSelected
并将其绑定到CheckBox.IsChecked
属性,该怎么办?
<DataGridTemplateColumn Header="Select" Width="*" IsReadOnly="False">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" >
<CheckBox IsChecked="{Binding IsTextSelected"} Click="TextSelected_Click"/>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
然后您可以检查哪些项目将IsTextSelected
设置为true。
private void TextSelected_Click(object sender, RoutedEventArgs e)
{
deltextindex.Clear();
for (int i = 0; i < textWmdg.Items.Count; i++)
{
var viewModel = textWmdg.Items[i] as YourViewModelType;
if (viewModel.IsTextSelected)
{
deltextindex.Add(i);
}
}
}