开始编辑后突出显示单元格的文本
我将数据库(MDB)的内容加载到dataTable
对象中,然后在datagrid
中显示它们,以便我可以编辑它们。一旦我开始编辑单元格(按 f2 ),则选择整个文本。
防止此标记?
我的datagrid
标记:
<DataGrid VirtualizingStackPanel.VirtualizationMode="Standard" Height="750" Width="1920" CanUserResizeColumns="True" CanUserReorderColumns="False" CanUserResizeRows="False" RowHeight="25" FrozenColumnCount="1" x:Name="DataGrid1" CanUserAddRows="False" CanUserSortColumns="False" AutoGenerateColumns="False" FontSize="13" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0,0,0,120" SelectionUnit="CellOrRowHeader" SelectionMode="Extended">
<DataGrid.Columns>
<DataGridTextColumn Width="450" Header=" Column1 " Binding="{Binding Column1}">
<DataGridTextColumn.EditingElementStyle>
<Style TargetType="TextBox">
<Setter Property="MaxLength" Value="150"></Setter>
</Style>
</DataGridTextColumn.EditingElementStyle>
</DataGridTextColumn>
<DataGridTextColumn Width="120" Header=" Column2 " Binding="{Binding Column2}">
<DataGridTextColumn.EditingElementStyle>
<Style TargetType="TextBox">
<Setter Property="MaxLength" Value="10"></Setter>
</Style>
</DataGridTextColumn.EditingElementStyle>
</DataGridTextColumn>
</DataGrid.Columns>
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<EventSetter Event="KeyDown" Handler="DATAGRID_Keydown" ></EventSetter>
</Style>
</DataGrid.RowStyle>
</DataGrid>
我的C#代码:
private void DATAGRID_Keydown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Tab | e.Key == Key.F2)
{
DataGrid1.BeginEdit();
}
}
I load the contents of a database (mdb) into a DataTable
object and display them in a DataGrid
so that I can edit them. As soon as I start editing a cell (pressing F2), the entire text is selected.
Is there a way to prevent this marking?
My DataGrid
markup:
<DataGrid VirtualizingStackPanel.VirtualizationMode="Standard" Height="750" Width="1920" CanUserResizeColumns="True" CanUserReorderColumns="False" CanUserResizeRows="False" RowHeight="25" FrozenColumnCount="1" x:Name="DataGrid1" CanUserAddRows="False" CanUserSortColumns="False" AutoGenerateColumns="False" FontSize="13" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0,0,0,120" SelectionUnit="CellOrRowHeader" SelectionMode="Extended">
<DataGrid.Columns>
<DataGridTextColumn Width="450" Header=" Column1 " Binding="{Binding Column1}">
<DataGridTextColumn.EditingElementStyle>
<Style TargetType="TextBox">
<Setter Property="MaxLength" Value="150"></Setter>
</Style>
</DataGridTextColumn.EditingElementStyle>
</DataGridTextColumn>
<DataGridTextColumn Width="120" Header=" Column2 " Binding="{Binding Column2}">
<DataGridTextColumn.EditingElementStyle>
<Style TargetType="TextBox">
<Setter Property="MaxLength" Value="10"></Setter>
</Style>
</DataGridTextColumn.EditingElementStyle>
</DataGridTextColumn>
</DataGrid.Columns>
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<EventSetter Event="KeyDown" Handler="DATAGRID_Keydown" ></EventSetter>
</Style>
</DataGrid.RowStyle>
</DataGrid>
My C# Code:
private void DATAGRID_Keydown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Tab | e.Key == Key.F2)
{
DataGrid1.BeginEdit();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
作为添加
准备
的处理程序的替代方法,您可以创建一个行为,该行为封装了用于移动textbox
的代码的行为,以使其可重复使用跨多个视图,无需复制每个实例的代码。您必须安装“ > nuget软件包,并为
datagrid
创建一个行为。该行为将处理程序添加到准备
的情况下,并将其设置为结尾。还有一个附件的属性,使您可以将列排除在行为之外。通过颠倒默认值,您可以默认情况下禁用该行为,并为每列明确启用它。要使用该行为,只需将其添加到
互动中的数据网格中。
As an alternative to adding a handler for
PreparingCellEdit
in code-behind, you could create a behavior that encapsulates the code for moving the caret of theTextBox
in order to make it reusable across multiple views without copy-pasting the code for each instance.You have to install the Microsoft.Xaml.Behaviors.Wpf NuGet package and create a behavior for
DataGrid
. The behavior adds a handler toPreparingCellEdit
and sets the caret to the end. There is also an attached property which enables you to exclude columns from the behavior. By inverting the default value, you could disable the behavior by default and enable it explicitly for each column.To use the behavior, simply add it to the data grid in the
Interaction.Behaviors
tag.您可以使用
准备
datagrid
:XAML
C#的准备事件(它将将光标放置在当前单元格文本的末尾),
您可以使用
textBox)e.editingElement).selectionStart = 0;
将光标放置在该单元格的原始文本的开头。you can use the
PreparingCellForEdit
event for theDataGrid
:XAML
C# (It will place the cursor at the end of the current cell text)
You can use use
((TextBox)e.EditingElement).SelectionStart = 0;
to place the cursor at the beginning of the original text of that cell.