WPF DataGrid - 如何自动退出编辑模式? [英] WPF DataGrid - How to automatically exit Edit Mode?

查看:2421
本文介绍了WPF DataGrid - 如何自动退出编辑模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经从Codeplex实施了WPF DataGrid 单击编辑
在该解决方案中,被点击的单元格被聚焦,并且该行被选择以实现
单击编辑DataGrid。

I have implemented WPF DataGrid Single-Click Editing from Codeplex. In that solution, the clicked cell is focused and the row is selected to achieve single-click editing of DataGrid. It worked great.

下面是代码:

private void DataGridCell_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        DataGridCell cell = sender as DataGridCell;
        if (cell != null && !cell.IsEditing && !cell.IsReadOnly)
        {
            if (!cell.IsFocused)
            {
                cell.Focus();
            }
            DataGrid dataGrid = FindVisualParent<DataGrid>(cell);
            if (dataGrid != null)
            {
                if (dataGrid.SelectionUnit != DataGridSelectionUnit.FullRow)
                {
                    if (!cell.IsSelected)
                        cell.IsSelected = true;
                }
                else
                {
                    DataGridRow row = FindVisualParent<DataGridRow>(cell);
                    if (row != null && !row.IsSelected)
                    {
                        row.IsSelected = true;
                    }
                }
            }
        }
    }    


$ b b

但我还希望我的DataGrid自动退出编辑模式(不按Enter键)
当单元格值更改时。例如,在编辑模式下,我在单元格中有一个组合框。当用户在组合框中选择一个值时,它将自动对所选值进行数据绑定。但是,然后用户仍然需要单击Enter退出编辑模式。如何自动退出编辑模式?

But I also want my DataGrid to automatically exit editing mode (without hitting Enter key) when a cell value is changed. For example, I have a combobox in the cell when in edit mode. When user selects a value in combobox, it will automatically databind the selected value. But then the user still need to click Enter to exit edit mode. How can I exit edit mode automatically?

我试过监听属性更改,并调用DataGrid的CommitEdit函数自动退出编辑模式。非常好,代码如下:

I've tried listening for property changes and call CommitEdit function of DataGrid to exit edit mode automatically. Works great and here's the code:

 void _gameCompareViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
    {
        if (e.PropertyName == "End Edit")
        {
            AlignGrid.CommitEdit();
        }

    }

但现在的单击编辑功能将不适用于当前单元格。
我必须先点击不同的行才能正常工作。
我想我想要的是当CommmitEdit被调用时,它会自动选择一个
不同的行。 (就像当你按Enter,它会去下一行)
任何建议家伙?请告诉我如何做的代码。我在这里为我的项目耗尽时间。

But now the Single-Click editing feature will not work for the current cell. I have to click a different row first in order for it to work. I think what I want is when CommmitEdit is called, it automatically selects a different row. (Like when you hit Enter, it will go to the next row) Any suggestions guys? Please show me codes on how to do this. Im running out of time here for my project.

感谢您的帮助。

推荐答案

从单元格编辑模板切换回单元格模板:

to flip from cell editing template back to cell template:

dataGrid.CancelEdit();

这篇关于WPF DataGrid - 如何自动退出编辑模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆