2016-11-29 73 views
2

真的,我有一個DataGrid和使用它的CellEditEnding事件。WPF DataGrid中,時刻關注電池如果e.Cancel =在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 

} 

回答

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