2016-11-29 150 views
2

私はDataGridを持ち、そのCellEditEndingイベントを使用しています。WPF DataGrid、CellEditEndingイベントでe.Cancel = trueの場合にフォーカスを保持

デフォルトでは、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 

} 

答えて

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

私はあなたを助けました。 幸運)

+0

:お返事ありがとうございます。コンパイルエラーが発生し、FindVisualChildByNameが見つかりません – nk1

関連する問題