主要是为了用户输入方便
按回车,选中下一列,text自动获取焦点,输入状态
获取控件实例 https://blog.csdn.net/m15188153014/article/details/48627757
textbox选中则全选 https://www.cnblogs.com/babietongtianta/p/3952214.html
private void dgContent_SelectionChanged(object sender, SelectionChangedEventArgs e) { DataGridTemplateColumn tempColumn = this.dgContent.Columns[4] as DataGridTemplateColumn; FrameworkElement element = this.dgContent.Columns[4].GetCellContent(this.dgContent.SelectedItem); if (element != null) { //把单元格元素转换为相应的控件,再从该控件中取值 TextBox ck = tempColumn.CellTemplate.FindName("tb", element) as TextBox; if (ck != null) { //if(ck.MouseDoubleClick!=null) ck.PreviewMouseDown += new MouseButtonEventHandler(LIKE_textBox_PreviewMouseDown);//注意,这个事件的注册必须在LIKE_textBox获得焦点之前 ck.GotFocus += new RoutedEventHandler(LIKE_textBox_GotFocus); ck.LostFocus += new RoutedEventHandler(LIKE_textBox_LostFocus); ck.Focus(); ck.PreviewMouseDown -= new MouseButtonEventHandler(LIKE_textBox_PreviewMouseDown);//注意,这个事件的注册必须在LIKE_textBox获得焦点之前 ck.GotFocus -= new RoutedEventHandler(LIKE_textBox_GotFocus); ck.LostFocus -= new RoutedEventHandler(LIKE_textBox_LostFocus); } } } void LIKE_textBox_LostFocus(object sender, RoutedEventArgs e) { (sender as TextBox).PreviewMouseDown += new MouseButtonEventHandler(LIKE_textBox_PreviewMouseDown); } void LIKE_textBox_PreviewMouseDown(object sender, MouseButtonEventArgs e) { (sender as TextBox).Focus(); e.Handled = true; } void LIKE_textBox_GotFocus(object sender, RoutedEventArgs e) { (sender as TextBox).SelectAll(); (sender as TextBox).PreviewMouseDown -= new MouseButtonEventHandler(LIKE_textBox_PreviewMouseDown); }