Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
349 views
in Technique[技术] by (71.8m points)

c# - Event for Select All: WPF Datagrid

I am using WPF data-grid. In data-grid user have column-headers and row-headers.

When column-headers and row-headers both of them are visible, in the top left corner we have one small square section available. (cross section in the top left corner where the column and row headers meet.) when we click on that it selects all the cells within data-grid. Is there any event for that? If not how can trap that event. Please guide me.

Do let me know if you need any other information regarding this problem.

Regards, Priyank

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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();
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...