Automatic editing of WPF datagrid content when datagrid-cell gets focus

后端 未结 5 715
悲哀的现实
悲哀的现实 2021-02-04 09:00

I have a datagrid in WPF with a DataGridTextColum and a DataGridTemplateColum.



        
5条回答
  •  滥情空心
    2021-02-04 09:22

    This approach works for me. It uses the fact that the DataGrid will always create a new instance of the template when the editing starts:

    
        
            
        
    
    

    and in the code behind:

    private void TextBox_Loaded(object sender, RoutedEventArgs e)
    {
        ((TextBox)sender).Focus();
        ((TextBox)sender).SelectAll();
    }
    

    As an added bonus, it also selects all text in the cell. It should work no matter how you enter the editing mode (double click, single click, pressing F2)

提交回复
热议问题