您好,不知道如何将光标放在 DatagridCell 内并选择所有文本
\n\n下面的代码将焦点设置在单元格上,开始编辑。但光标不在单元格内,因此用户无法开始键入文本。此外,未选择,因此用户必须手动选择文本,而不是直接替换值。
\n\nMainwindow.xaml.cs:
\n\n private void GrdLignes_SelectionChanged(object sender, SelectionChangedEventArgs e)\n {\n foreach (var c in GrdLignes.SelectedCells)\n {\n if (c.Column.Header.ToString() == "Quantit\xc3\xa9 Livr\xc3\xa9e")\n {\n var cellContent = c.Column.GetCellContent(c.Item);\n if (cellContent != null)\n {\n var dc = (DataGridCell)cellContent.Parent;\n dc.Focus();\n dc.IsEditing = true;\n }\n }\n }\n }\n
Run Code Online (Sandbox Code Playgroud)\n\n\n\n编辑:我说光标=闪烁插入符
\n您需要等到TextBlock
单元格中的 被 替换TextBox
。
定义EditingElementStyle
并处理Loaded
的事件TextBox
:
<DataGrid x:Name="GridLignes" ...>\n <DataGrid.Resources>\n <Style x:Key="tbStyle" TargetType="TextBox">\n <EventSetter Event="Loaded" Handler="OnLoaded" />\n </Style>\n </DataGrid.Resources>\n <DataGrid.Columns>\n <DataGridTextColumn Header="Quantit\xc3\xa9 Livr\xc3\xa9e" Binding="{Binding Qty}" EditingElementStyle="{StaticResource tbStyle}" />\n ...\n </DataGrid.Columns>\n</DataGrid>\n
Run Code Online (Sandbox Code Playgroud)\n\nprivate void OnLoaded(object sender, RoutedEventArgs e)\n{\n TextBox textBox = sender as TextBox;\n Keyboard.Focus(textBox);\n textBox.CaretIndex = textBox.Text.Length;\n textBox.SelectAll();\n}\n
Run Code Online (Sandbox Code Playgroud)\n
归档时间: |
|
查看次数: |
2705 次 |
最近记录: |