Automatic editing of WPF datagrid content when datagrid-cell gets focus

后端 未结 5 802
悲哀的现实
悲哀的现实 2021-02-04 09:00

I have a datagrid in WPF with a DataGridTextColum and a DataGridTemplateColum.



        
5条回答
  •  天涯浪人
    2021-02-04 09:27

    Hisham's suggestion works perfectly for me, but I would use OnCurrentCellChanged instead, since OnSelectedCellsChanged will not work when the SelectionUnit is CellOrRowHeader. In the latter case BeginEdit() would only be triggered when selection moves to a cell in another row. Stepping left or right will not trigger the event at all.

    Also, it is probably advisable to add a DependencyProperty to the custom control and check against it before triggering BeginEdit(), to prevent this behavior if so desired (as done by other DataGrids, such as XCeed). But this is not a critic - just something that I usually do.

        protected override void OnCurrentCellChanged(EventArgs e)
        {
            // Make sure a cell is selected and only enter edit mode
            // if this is the desired behavior 
            if (CurrentCell != null && EditTrigger == EditTriggers.CellsCurrent)
            {
                this.BeginEdit();
            }
            base.OnCurrentCellChanged(e);
        }
    

提交回复
热议问题