• DataGrid研究笔记-1


    下面是我研究DataGrid的一点心得:

    首先wpf datagrid binding,网上的列子多不胜数,这里就没必要再列举了,下面主要说的是datagrid的焦点控制。

    默认的datagrid焦点控制都是由Tab控制的,按一下tab,焦点移动到下一个.但是其他的控件的焦点控制是用回车控制的,不是很统一。接下来我所要做的就是用回车键来控制datagrid的焦点移动。

    先来说说我网上搜到的方法:

    public class ExDataGrid:DataGrid
        {
            protected override void OnPreviewKeyDown(System.Windows.Input.KeyEventArgs e)
            {
                base.OnPreviewKeyDown(e);
            }
        }

    override了OnPreviewKeyDown方法,使用基本控件的base.OnPreviewKeyDown,即回车控制焦点,但是有些问题,碰到表格里是datatemplate或者是combobox的时候,移动会有些问题。

    网上也没有搜到其他方法,没办法只能求助于reflector看看微软是怎么实现tab焦点控制的。

    UIElement focuseElement = Keyboard.FocusedElement as UIElement;
    FocusNavigationDirection focusNavigationDirection = flag2 ?
                            FocusNavigationDirection.Previous : FocusNavigationDirection.Next;
                        TraversalRequest request = new TraversalRequest(focusNavigationDirection);
                        request.Wrapped = true;
    focuseElement.MoveFocus(request)

    不过可恶的是,除了wpf提供OnKeyDown可以override之外,其他方法都是private的,没办法只能照着他的思路,自己写一套了。

    private void OnEnterKeyDown(System.Windows.Input.KeyEventArgs e)
            {
                _isMouseClick = false;
                DataGridCell currentCellContainer = null;
                if (this.CurrentCell.Column.Header!=null)
                {
                    currentCellContainer = this.TryFindCell(this.CurrentCell);               
                }
                else
                {
                    currentCellContainer = this.CurrentCellContainer;
                }
                
                if (currentCellContainer != null)
                {
                    bool isEditing = currentCellContainer.IsEditing = true;
                    bool flag2 = (e.KeyboardDevice.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift;
                    UIElement focuseElement = Keyboard.FocusedElement as UIElement;
                    var myDataGridTextColumn = currentCellContainer.Column as MyDataGridTextColumn;
                    
                    ContentElement element2 = (focuseElement == null) ? (Keyboard.FocusedElement as ContentElement) : null;
                    if (focuseElement != null || element2 != null)
                    {
                        e.Handled = true;
                        FocusNavigationDirection focusNavigationDirection = flag2 ?
                            FocusNavigationDirection.Previous : FocusNavigationDirection.Next;
                        TraversalRequest request = new TraversalRequest(focusNavigationDirection);
                        request.Wrapped = false;
                        if (focuseElement != null && focuseElement.MoveFocus(request) || element2 != null && element2.MoveFocus(request))
                        {
                            _preUIElement = focuseElement;
                            this.BringIntoView();
                            if (isEditing && flag2 && Keyboard.FocusedElement == currentCellContainer)
                                currentCellContainer.MoveFocus(request);
                            if (base.IsGrouping && isEditing)
                            {
                                DataGridCell cellForSelectAndEditOnFocusMove = this.GetCellForSelectAndEditOnFocusMove();
                                if (cellForSelectAndEditOnFocusMove != null && GetRowDataItem(cellForSelectAndEditOnFocusMove) == GetRowDataItem(currentCellContainer))
                                {
                                    DataGridCell cell3=this.TryFindCell(GetRowDataItem(cellForSelectAndEditOnFocusMove),cellForSelectAndEditOnFocusMove.Column);
                                    if (cell3 == null)
                                    {
                                        base.UpdateLayout();
                                        cell3 = this.TryFindCell(GetRowDataItem(cellForSelectAndEditOnFocusMove), cellForSelectAndEditOnFocusMove.Column);
                                    }
                                    if (cell3 != null && cell3 != cellForSelectAndEditOnFocusMove)
                                        cell3.Focus();
                                }
                            }
                            
                            this.SelectAndEditOnFocusMove(e, currentCellContainer, isEditing, false, true,focuseElement);
                        }
                    }
                }
            }

    然后就可以override OnKeyDown

    protected override void OnKeyDown(System.Windows.Input.KeyEventArgs e)
            {
                if (this.Items.IndexOf(this.CurrentItem) == this.Items.Count - 1
                    && this.Columns.IndexOf(this.CurrentColumn) == this.Columns.Count - 1)
                {
                    return;
                }
                if (e.Key == Key.Enter)
                    OnEnterKeyDown(e);
                //base.OnKeyDown(e);
            }


    这样基本的功能就完成了。

  • 相关阅读:
    谬论之程序猿的眼光看世界
    phpStudy-坑爹的数据库管理器-phpMyAdmin的默认用户名和密码
    解决Delphi 2010启动时出现cannot create xxxxEditorLineEnds.ttr问题
    数据库中增加操作insert into的用法和查询select的用法
    TRichEdit怎样新增的内容到最后一行?
    BCB将RichEdit光标移到最后一行
    怎么把焦点放在RichEdit的最后一行
    Panel自动变颜色
    怎么判断pagecontrol下的TabSheet是否打开还是关闭求答案
    用Setup Factory7.0怎样打包delphi的BDE?
  • 原文地址:https://www.cnblogs.com/gavinhuang/p/3305406.html
Copyright © 2020-2023  润新知