扩展WPF的DataGrid按方向键移动焦点

WPF的DataGrid默认的移动行为如下:

(1)当前单元格不处于编辑状态时可使用方向键移动焦点。

(2)当前单元格处于编辑状态时不可使用方向键移动焦点;
按Enter键,当前单元格退出编辑状态,焦点向下移动一格;
按Ctrl+Enter键,当前单元格退出编辑状态,焦点向上移动一格;
按Tab键,当前单元格退出编辑状态,焦点向右移动一格,并进入编辑状态;
按Shift+Tab键,当前单元格退出编辑状态,焦点向左移动一格,并进入编辑状态;

“运营管理3.0″的用户普遍电脑操作水平低下,Office是它们操作的最多也是最智能的软件,它们认为所有的软件都必须是Office一样地操作,否则就给差评,所以它们要求”运营管理”的操作方式尽量与Excel相同.

为了实现WPF的DataGrid的移动方式与Excel相同,需要重写DataGrid的OnPreviewKeyDown事件:

 

public class ExDataGrid : DataGrid
    {
        protected override void OnPreviewKeyDown(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();
 
                    string mess = ex.Message;
                    if (ex.InnerException != null)
                        mess += "nn" + ex.InnerException.Message;
                    MessageBox.Show(mess);
                }
            }
 
            base.OnPreviewKeyDown(e);
        }
    }
View Code

 


然后在使用DataGrid的地方换成已重写的ExDataGrid就可以了.

转载自:http://www.yuzifu.net/index.php/2010/11/%E6%89%A9%E5%B1%95wpf%E7%9A%84datag


转载于:https://www.cnblogs.com/nov5026/p/4798951.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WPF DataGrid是一种用于显示和编辑数据的强大控件,可以通过一些简单的步骤实现拖动移动位置。 首先,在XAML中添加一个DataGrid控件并绑定数据源。可以使用ItemsSource属性将数据源绑定到DataGrid上,例如: <Grid> <DataGrid x:Name="myDataGrid" ItemsSource="{Binding MyData}" /> </Grid> 接下来,我们需要对DataGrid进行一些设置,使其可以支持拖动功能。为了实现这一点,我们需要在DataGrid的样式中添加一些代码。我们可以为DataGrid添加一个MouseMove事件处理程序,以识别鼠标左键按下并移动的事件: <DataGrid x:Name="myDataGrid" ItemsSource="{Binding MyData}" MouseMove="DataGrid_MouseMove"/> 然后,在代码中添加MouseMove事件的处理程序。在这个处理程序中,我们可以使用PreviewMouseLeftButtonDown事件来捕获鼠标左键按下的位置,并将其保存在一个变量中: private Point _startPoint; private void DataGrid_MouseMove(object sender, MouseEventArgs e) { if (e.LeftButton == MouseButtonState.Pressed) { Point position = e.GetPosition(null); if (Math.Abs(position.X - _startPoint.X) > SystemParameters.MinimumHorizontalDragDistance || Math.Abs(position.Y - _startPoint.Y) > SystemParameters.MinimumVerticalDragDistance) { DataGridRow dataGridRow = FindVisualParent<DataGridRow>(e.OriginalSource as UIElement); if (dataGridRow != null) { DataGrid grid = sender as DataGrid; if (grid != null && grid.SelectedItems != null && grid.SelectedItems.Count == 1) { DragDrop.DoDragDrop(dataGridRow, grid.SelectedItem, DragDropEffects.Move); } } } } } 最后,在DataGrid上添加一个Drop事件,在该事件中处理数据脱出和放置的逻辑。在Drop事件中,我们可以从拖动的数据中获取所需要的信息,并根据需要进行操作,例如重新排序数据: <DataGrid x:Name="myDataGrid" ItemsSource="{Binding MyData}" MouseMove="DataGrid_MouseMove" Drop="DataGrid_Drop"/> private void DataGrid_Drop(object sender, DragEventArgs e) { DataGridRow row = FindVisualParent<DataGridRow>(e.OriginalSource as UIElement); if (row != null) { int newIndex = row.GetIndex(); object data = e.Data.GetData(typeof(MyDataItem)); // 重新排序数据 // ... } } 通过以上步骤,我们可以实现在WPF DataGrid中拖动移动位置的功能。当我们按下鼠标左键并移动时,我们可以通过拖拽选中的行来重新排序数据或实现其它操作。希望这个简单的解答能对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值