WPF的DataGrid编辑单元格回车往右拐的方法



    最后一个可编辑的单元格按下回车时增加一行个人感觉太繁琐了点  最好是直接给Grid加一行 而不是给对应的集合加一行,寻求更好的解决方案!

        /// <summary>
        /// Grid最后一列加行和往右拐
        /// </summary>
        /// <typeparam name="Tresult"></typeparam>
        /// <param name="dataGrid"></param>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <param name="li"></param>
        public static void Grid_Right<Tresult>(this DataGrid dataGrid, object sender, KeyEventArgs e) where Tresult : class, new()
        {
            var uie = e.OriginalSource as UIElement;
            if (e.Key == Key.Enter)
            {
                e.Handled = true;
                uie.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
                dataGrid.BeginEdit();
                if (dataGrid.CurrentColumn != null)
                {
                    if (dataGrid.CurrentColumn.IsReadOnly == true)
                    {

                        for (int b = dataGrid.CurrentColumn.DisplayIndex; b < dataGrid.Columns.Count; b++)
                        {
                            for (int k = 0; k < dataGrid.Columns.Count; k++)
                            {
                                if (dataGrid.Columns[k].DisplayIndex == b)
                                {
                                    if (dataGrid.Columns[k].IsReadOnly == false && dataGrid.Columns[k].Visibility == Visibility.Visible)
                                    {
                                        dataGrid.CurrentColumn = dataGrid.Columns[k];
                                        dataGrid.SelectedItem = dataGrid.CurrentItem;
                                        dataGrid.BeginEdit();
                                        return;
                                    }
                                    else if (k == dataGrid.Columns.Count - 1 && (dataGrid.Columns[k].IsReadOnly == true || dataGrid.Columns[k].Visibility == Visibility.Hidden))
                                    {
                                        if (dataGrid.SelectedIndex != dataGrid.Items.Count - 1)
                                        {
                                            dataGrid.SelectedIndex = dataGrid.SelectedIndex + 1;
                                            dataGrid.CurrentColumn = dataGrid.Columns[0];
                                            dataGrid.CurrentItem = dataGrid.SelectedItem;
                                            b = 0;
                                        }
                                        else
                                        {
                                           //PropertyInfo p = dataGrid.CurrentItem.GetType();
                                            ModelObservableCollection<Tresult> li = dataGrid.ItemsSource as ModelObservableCollection<Tresult>;
                                            li.Insert(dataGrid.SelectedIndex + 1, new Tresult());
                                            dataGrid.SelectedIndex = dataGrid.SelectedIndex + 1;
                                            dataGrid.CurrentColumn = dataGrid.Columns[0];
                                            dataGrid.CurrentItem = dataGrid.SelectedItem;
                                            b = 0;
                                        }

                                    }
                                }
                            }

                        }
                    }
                }
            }
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值