WPF DataGrid,如果在CellEditEnding事件中e.Cancel = true,则将焦点保持在单元格上

时间:2016-11-29 07:40:35

标签: c# wpf datagrid

我有一个数据网格并使用它的CellEditEnding事件。

默认情况下在CellEditEnging事件中,如果我取消提交,它允许将光标移动到其他单元格或其他行。

如果我取消编辑,我的查询还有其他方法,除非他在CellEditEnging事件中更正输入的行,否则不允许用户移动其他单元格或其他行。

 MainWindow.xaml.cs Code

    public MainWindow()
    {
        InitializeComponent();

        List<Student> sList = new List<Student>();
        sList.Add(new Student() { Name = "Amar" });
        sList.Add(new Student() { Name = "Sagar" });
        sList.Add(new Student() { Name = "Kiran" });
        dg1.ItemsSource = sList;

        dg1.CellEditEnding += Dg1_CellEditEnding;
    }

    private void Dg1_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
    {
        TextBox txtBox = e.EditingElement as TextBox;

        if (txtBox != null && txtBox.Text.Equals("Amar"))
            e.Cancel = true; //my requirement is,once i cancel ,focus should not move to other rows or other cells,it should be remain on this cell

    }
}
public class Student : INotifyPropertyChanged
{
    private string name;
    public string Name
    {
        get { return name; }
        set
        {
            name = value;
            OnPropertyChanged("Name");
        }
    }


    #region INotifyPropertyChanged

    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this,
                new PropertyChangedEventArgs(propertyName));
        }
    }


    #endregion

}

3 个答案:

答案 0 :(得分:1)

我遇到了同样的问题,并且找到了答案:

if (hasError)
{
    e.Cancel = true;

    (sender as DataGrid).Dispatcher.BeginInvoke((Action)(() =>
    {
        (sender as DataGrid).SelectedIndex = e.Row.GetIndex(); //select current row
        ((System.Windows.UIElement)e.EditingElement.Parent).Focus(); //focus current cell
    }));

}

答案 1 :(得分:0)

尝试清除TextBox并以编程方式移动焦点。我给你举个例子,怎么做:

        private void PlanningDataGrid_KeyUp(object sender, KeyEventArgs e)
    {
        if (e.Key == System.Windows.Input.Key.Tab)
        {
            try
            {
                if (PlanningDataGrid.FindVisualChildByName<ComboBox>("EventTypeComboBox") != null)
                {
                    Keyboard.Focus(PlanningDataGrid.FindVisualChildByName<ComboBox>("EventTypeComboBox"));
                }
                if (PlanningDataGrid.FindVisualChildByName<ComboBox>("shopСomboBox") != null)
                {
                    Keyboard.Focus(PlanningDataGrid.FindVisualChildByName<ComboBox>("shopСomboBox"));
                }
                if (PlanningDataGrid.FindVisualChildByName<ComboBox>("oilfieldСomboBox") != null)
                {
                    Keyboard.Focus(PlanningDataGrid.FindVisualChildByName<ComboBox>("oilfieldСomboBox"));
                }
                if (PlanningDataGrid.FindVisualChildByName<ComboBox>("wellClusterСomboBox") != null)
                {
                    Keyboard.Focus(PlanningDataGrid.FindVisualChildByName<ComboBox>("wellClusterСomboBox"));
                }
                if (PlanningDataGrid.FindVisualChildByName<ComboBox>("oilWellСomboBox") != null)
                {
                    Keyboard.Focus(PlanningDataGrid.FindVisualChildByName<ComboBox>("oilWellСomboBox"));
                }
                //if ()
            }
            catch (Exception) { }
        }
    }
希望,我帮助了你。 祝你好运)

答案 2 :(得分:0)

尝试以这种方式在e.Handled事件中将true设置为PreviewLostKeyboardFocus

private void DataGrid_PreviewLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
    if (e.OldFocus is TextBox textBox)
    { 
        // some validation may be applied here to textBox.Text
        e.Handled = true;
    }
}