简体   繁体   中英

WPF DataGrid, Keep Focus on cell if e.Cancel = true in CellEditEnding event

I have a datagrid and using it's CellEditEnding event.

By default In CellEditEnging event if I cancel the commit,it allows to move cursor to other cells or other rows.

My query is there any other way if I cancel the edit,user should not be allowed to move other cells or other rows unless he corrects the entered one in CellEditEnging event.

 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

}

I'm having the same question and I have found an answer:

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
    }));

}

Try just to clear the TextBox and move focus programmatically. Lower i give you example, how to do it:

        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) { }
        }
    }

Hope, I helped you. Good luck)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM