<DataGridTemplateColumn Header="Template" > <DataGridTemplateColumn.CellTemplate> <DataTemplate> <TextBlock Text="{Binding xxx}"/> </DataTemplate> </DataGridTemplateColumn.CellTemplate> <DataGridTemplateColumn.CellEditingTemplate> <DataTemplate> <TextBox x:Name="tbxxx" Text="{Binding xxx}" FocusManager.FocusedElement="{Binding RelativeSource={RelativeSource Self}}"></TextBox> </DataTemplate> </DataGridTemplateColumn.CellEditingTemplate> </DataGridTemplateColumn>
1: TextBox 自動獲取焦點
<TextBox FocusManager.FocusedElement="{Binding RelativeSource={RelativeSource Self}}"></TextBox>
2:TextBox獲取焦點全選文本 :https://www.cnblogs.com/akiing/p/9717270.html
WPF使用DataGrid 時,如果使用 DataGridTextColumn 列,進入編輯模式,點擊後會直接將文本框全選。
但是如果使用 DataGridTemplateColumn 列時,DataTemplate 下綁定 TextBox,進入編輯模式並不會全選。
如果在模板列中的TextBox使用1方式,能使控件獲取焦點(讓鼠標進入TextBox)但是不會觸發任何事件,比如 GotFocus 、GotKeyboardFocus等
如果在模板列中的TextBox使用2方式,能使控件在獲取焦點時全選,但是相比較DataGrid的DataGridTextColumn 方式,需要多點擊一次。
另外在DataGrid 控件的 BeginningEdit 、CellEditEnding 事件中獲取不到 CellEditingTemplate 模板中的控件 TextBox.。
做個記錄,繼續尋找解決方案中...