开始编辑后突出显示单元格的文本

发布于 2025-02-13 12:31:51 字数 2168 浏览 1 评论 0原文

我将数据库(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.

Left (actual): All text is selected on edit. Right (expected): Caret is moved to end on edit.

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

久随 2025-02-20 12:31:51

作为添加准备 的处理程序的替代方法,您可以创建一个行为,该行为封装了用于移动textbox的代码的行为,以使其可重复使用跨多个视图,无需复制每个实例的代码。

您必须安装“ > nuget软件包,并为datagrid创建一个行为。该行为将处理程序添加到准备 的情况下,并将其设置为结尾。还有一个附件的属性,使您可以将列排除在行为之外。通过颠倒默认值,您可以默认情况下禁用该行为,并为每列明确启用它。

public class PrepareTextEditBehavior : Behavior<DataGrid>
{
   public static readonly DependencyProperty MoveCaretToEndProperty = DependencyProperty.RegisterAttached(
      "MoveCaretToEnd", typeof(bool), typeof(PrepareTextEditBehavior), new PropertyMetadata(true));

   public static bool GetMoveCaretToEnd(DependencyObject obj)
   {
      return (bool)obj.GetValue(MoveCaretToEndProperty);
   }

   public static void SetMoveCaretToEnd(DependencyObject obj, bool value)
   {
      obj.SetValue(MoveCaretToEndProperty, value);
   }

   protected override void OnAttached()
   {
      base.OnAttached();
      AssociatedObject.PreparingCellForEdit += OnPreparingCellForEdit;
   }

   protected override void OnDetaching()
   {
      base.OnDetaching();
      AssociatedObject.PreparingCellForEdit -= OnPreparingCellForEdit;
   }

   private static void OnPreparingCellForEdit(object sender, DataGridPreparingCellForEditEventArgs e)
   {
      if (!GetMoveCaretToEnd(e.Column))
         return;

      var textBox = (TextBox)e.EditingElement;
      textBox.CaretIndex = textBox.Text.Length;
   }
}

要使用该行为,只需将其添加到互动中的数据网格中。

<DataGrid ...>
   <b:Interaction.Behaviors>
      <local:PrepareTextEditBehavior/>
   </b:Interaction.Behaviors>
   <DataGrid.Columns>

      <DataGridTextColumn ...>
         <!-- ...column markup. -->
         <!-- ...the behavior is applied here by default. -->
      </DataGridTextColumn>

      <DataGridTextColumn ...
                          local:PrepareTextEditBehavior.MoveCaretToEnd="False">
         <!-- ...column markup. -->
         <!-- ...the behavior is NOT applied here explicitly. -->
      </DataGridTextColumn>
   </DataGrid.Columns>

</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 the TextBox 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 to PreparingCellEdit 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.

public class PrepareTextEditBehavior : Behavior<DataGrid>
{
   public static readonly DependencyProperty MoveCaretToEndProperty = DependencyProperty.RegisterAttached(
      "MoveCaretToEnd", typeof(bool), typeof(PrepareTextEditBehavior), new PropertyMetadata(true));

   public static bool GetMoveCaretToEnd(DependencyObject obj)
   {
      return (bool)obj.GetValue(MoveCaretToEndProperty);
   }

   public static void SetMoveCaretToEnd(DependencyObject obj, bool value)
   {
      obj.SetValue(MoveCaretToEndProperty, value);
   }

   protected override void OnAttached()
   {
      base.OnAttached();
      AssociatedObject.PreparingCellForEdit += OnPreparingCellForEdit;
   }

   protected override void OnDetaching()
   {
      base.OnDetaching();
      AssociatedObject.PreparingCellForEdit -= OnPreparingCellForEdit;
   }

   private static void OnPreparingCellForEdit(object sender, DataGridPreparingCellForEditEventArgs e)
   {
      if (!GetMoveCaretToEnd(e.Column))
         return;

      var textBox = (TextBox)e.EditingElement;
      textBox.CaretIndex = textBox.Text.Length;
   }
}

To use the behavior, simply add it to the data grid in the Interaction.Behaviors tag.

<DataGrid ...>
   <b:Interaction.Behaviors>
      <local:PrepareTextEditBehavior/>
   </b:Interaction.Behaviors>
   <DataGrid.Columns>

      <DataGridTextColumn ...>
         <!-- ...column markup. -->
         <!-- ...the behavior is applied here by default. -->
      </DataGridTextColumn>

      <DataGridTextColumn ...
                          local:PrepareTextEditBehavior.MoveCaretToEnd="False">
         <!-- ...column markup. -->
         <!-- ...the behavior is NOT applied here explicitly. -->
      </DataGridTextColumn>
   </DataGrid.Columns>

</DataGrid>
伪装你 2025-02-20 12:31:51

您可以使用准备datagrid

XAML

<DataGrid ... PreparingCellForEdit="DataGrid_PreparingCellForEdit">

C#的准备事件(它将将光标放置在当前单元格文本的末尾),

private void DataGrid_PreparingCellForEdit(object sender, DataGridPreparingCellForEditEventArgs e)
{
    ((TextBox)e.EditingElement).SelectionStart = ((TextBox)e.EditingElement).Text.Length;
}

您可以使用textBox)e.editingElement).selectionStart = 0;将光标放置在该单元格的原始文本的开头。

you can use the PreparingCellForEdit event for the DataGrid:

XAML

<DataGrid ... PreparingCellForEdit="DataGrid_PreparingCellForEdit">

C# (It will place the cursor at the end of the current cell text)

private void DataGrid_PreparingCellForEdit(object sender, DataGridPreparingCellForEditEventArgs e)
{
    ((TextBox)e.EditingElement).SelectionStart = ((TextBox)e.EditingElement).Text.Length;
}

You can use use ((TextBox)e.EditingElement).SelectionStart = 0; to place the cursor at the beginning of the original text of that cell.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文