The datagrid handles the routed command ApplicationCommand.SelectAll, so if the grid has focus and your press Ctrl-A, or you click the corner button, all cells are selected.
You can handle this command yourself by adding a CommandBinding in xaml:
<DataGrid x:Name="dataGrid" .../>
<DataGrid.CommandBindings>
<CommandBinding Command="ApplicationCommands.SelectAll" Executed="SelectAll_Executed"/>
</DataGrid.CommandBindings>
Or you can add the command binding in code:
public MyControl(){
InitializeComponent();
...
dataGrid.CommandBindings.Add(new CommandBinding(ApplicationCommands.SelectAll, SelectAll_Executed));
}
However, there can only be a single handler for a routed command, so by default adding this handler this will prevent select all from working in the datagrid.
In your handler you need therefore to call SelectAll.
private void SelectAll_Executed(object sender, ExecutedRoutedEventArgs e)
{
Debug.WriteLine("Executed");
dataGrid.SelectAll();
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…