我有以下数据网格:
<DataGrid x:Name="dataGridOrderItems" Margin="4,10,2,70"
VerticalGridLinesBrush="LightGray" HorizontalGridLinesBrush="LightGray" AlternatingRowBackground="Beige" AlternationCount="2"
SelectionMode="Single" SelectionUnit="FullRow"
AutoGenerateColumns="False" IsReadOnly="False" SelectionChanged="dataGridOrderItems_SelectionChanged">
<DataGrid.Columns>
<DataGridTextColumn Header="Resource Id" Binding="{Binding ResourceId}" />
<DataGridTextColumn Header="Resource Name" Binding="{Binding DisplayTitle}" />
<DataGridTextColumn Header="Quantity" Binding="{Binding Quantity}" />
<DataGridTextColumn Header="Type" Binding="{Binding ResourceType}"/>
<DataGridTextColumn Header="Order Date" Binding="{Binding OrderDate, StringFormat=\{0:d\}}" />
<DataGridTemplateColumn Header="Select" CellTemplate="{StaticResource template}"/>
</DataGrid.Columns>
<DataGrid.ContextMenu>
<ContextMenu >
<MenuItem Header="Select All" Click="SelectAllDatagridOrderItems" />
</ContextMenu>
</DataGrid.ContextMenu>
</DataGrid>
绑定到以下数据源:
public class CustomerOrdersEntity
{
public int ResourceId { get; set; }
public string DisplayTitle { get; set; }
public int Quantity { get; set; }
public string ResourceType { get; set; }
public int OrderId { get; set; }
public string OrderDate { get; set; }
public bool IsChecked { get; set; }
}
我尝试使用以下方法实现选择所有项目:
public void SelectAllDatagridOrderItems(object sender, RoutedEventArgs e)
{
if (dataGridOrderItems.ItemsSource == null)
{
MessageBox.Show(
"No resources are currently loaded! Please select a customer to continue.",
"No resources loaded",
MessageBoxButton.OK,
MessageBoxImage.Asterisk);
return;
}
dataGridOrderItems.SelectAll();
}
然而,这会引发以下异常:
未处理的类型&#39; System.NotSupportedException&#39;发生了 在PresentationFramework.dll
中
我该如何处理?我想迭代itemsource并将IsChecked
更新为每行true
,但这看起来似乎不必要了,我确信必须有一个更简单的解决方案,我错过了?