Extend WPF's DataGrid to move focus by arrow key

Source: Internet
Author: User

The default move behavior for WPF's DataGrid is as follows:

(1) You can use the arrow keys to move focus when the current cell is not in the editing state.

(2) You cannot use the arrow keys to move the focus while the current cell is in the editing state;
Press ENTER, the current cell exits the edit state, and the focus moves down one cell;
Pressing the Ctrl+enter key, the current cell exits the edit state, and the focus moves up one grid;
Pressing the TAB key, the current cell exits the edit state, the focus moves one cell to the right, and enters the editing state;
Pressing the Shift+tab key, the current cell exits the edit state, the focus moves to the left one pane, and enters the editing state;

"Operations management 3.0″ users generally have low levels of computer operations, Office is the most and the most intelligent software they operate, they think all the software must be office-like operation, otherwise it will be poor, so they require" operations management "to operate as much as Excel.

In order for WPF to move the DataGrid in the same way as Excel, you need to rewrite the DataGrid's Onpreviewkeydown event:

 Public classExdatagrid:datagrid {protected Override voidOnpreviewkeydown (KeyEventArgs e) {if(E.key = = Key.left | | e.key = Key.right | | e.key = KEY.UP | | e.key = =key.down) {Try                {                    Base.                Commitedit (); }                Catch(Exception ex) {Base.                     CancelEdit (); stringMess =Ex.                    Message; if(ex. InnerException! =NULL) Mess+="nn"+Ex.                    Innerexception.message;                MessageBox.Show (mess); }            }             Base.        Onpreviewkeydown (e); }    }
View Code


Then replace the Exdatagrid with the rewritten ones where the DataGrid is used.

Reprinted from: Http://www.yuzifu.net/index.php/2010/11/%E6%89%A9%E5%B1%95wpf%E7%9A%84datag


Extend WPF's DataGrid to move focus by arrow key

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.