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

查看:170
本文介绍了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;
                    }
                }
            }
        }
    }    

但是,当单元格值更改时,我也希望我的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被调用时,它会自动选择
不同的行。 (喜欢当你输入,它会去下一行)
任何建议的人?请告诉我如何做这个代码。我的项目在这里用完了。

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天全站免登陆