当数据网格单元获得焦点时自动编辑 WPF 数据网格内容



我在WPF中有一个数据网格,带有DataGridTextColumDataGridTemplateColum

<DataGridTextColumn Width="4*" IsReadOnly="True" x:Name="dataGridColumnDescription" 
Header="Description" Binding="{Binding Description}">
</DataGridTextColumn>
<DataGridTemplateColumn CellStyle="{StaticResource CellEditing}" IsReadOnly="False" Width="*" Header="Value" 
CellEditingTemplateSelector="{StaticResource myCellEditingTemplateSelectorValue}" 
CellTemplateSelector="{StaticResource myCellTemplateSelectorValue}">
</DataGridTemplateColumn>

CellTemplateSelectors 返回一个带有 TextBlock 的数据模板,用于单元格模板或用于单元格编辑的文本框!

<DataTemplate x:Key="dGridStringValueTemplate">
    <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Text="{Binding Path=Value}"/>
</DataTemplate>
<DataTemplate x:Key="dGridStringValueTemplateEditing">
    <TextBox TextAlignment="Center" VerticalAlignment="Center" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" BorderThickness="1" Text="{Binding Path=Value, UpdateSourceTrigger=LostFocus}"/>
</DataTemplate>

现在,我想在 DataGridCell 获得焦点时自动聚焦文本框。用户应该能够编辑文本框内容,而无需双击单元格。

我找到了这篇文章:

DataGrid 提示和技巧:单击编辑我可以在哪里获取当前数据网格单元,但是如何访问内容以使文本框具有编辑内容的焦点?

这是我的风格:

<Style x:Key="CellEditing" TargetType="{x:Type DataGridCell}">
    <EventSetter Event="PreviewMouseLeftButtonDown" Handler="myDataGridMain_PreviewMouseLeftButtonDown"></EventSetter>
</Style>

这是我的事件处理程序:

private void myDataGridMain_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    DataGridCell cell = sender as DataGridCell;     // cell ist not null
    DataGridTemplateColumn col = cell.Column as DataGridTemplateColumn; //col is not null
    DataTemplate template = col.CellTemplate;  //this is null
}

如何使用该事件处理程序获取文本框?

似乎有效:

    <DataGrid ItemsSource="{Binding Items}" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTemplateColumn>
                <DataGridTemplateColumn.CellEditingTemplate>
                    <DataTemplate>
                        <TextBox  FocusManager.FocusedElement="{Binding RelativeSource={RelativeSource Self}}"></TextBox>
                    </DataTemplate>
                </DataGridTemplateColumn.CellEditingTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>

这种方法对我有用。它使用的事实,即DataGrid将始终在编辑开始时创建模板的新实例:

<DataGridTemplateColumn.CellEditingTemplate>
    <DataTemplate>
        <TextBox Text="{Binding MyProperty}" 
                 Loaded="TextBox_Loaded"></TextBox>
    </DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>

在后面的代码中:

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

作为额外的好处,它还选择单元格中的所有文本。无论您如何进入编辑模式(双击,单击,按F2),它都应该有效

我做到了,不是最好的解决方案,但它有效......当Cell获得焦点时,我将其设置为编辑模式。

private void myDataGridMain_OnFocus(object sender, RoutedEventArgs e)
{
    DataGridCell cell = sender as DataGridCell;
    if (cell != null)
        cell.IsEditing = true;
    //var test = FindVisualChild<TextBlock>(cell);
}

在 Keydown 上,我搜索视觉子项并给出焦点。

private void myDataGridMain_KeyDown(object sender, KeyEventArgs e)
        {
            DataGridCell cell = sender as DataGridCell;
            if (e.Key == Key.Enter)
            {   //give cell the focus
                cell.Focus();
            }
            else
            {
                if ((cell != null))
                {
                    TextBox textbox = FindVisualChild<TextBox>(cell);
                    if (textbox != null)
                    {   //TextBox has benn found
                        if ((textbox as TextBox).IsFocused == false)
                        {
                            (textbox as TextBox).SelectAll();
                        }
                        (textbox as TextBox).Focus();
                    }
                    CheckBox chkbox = FindVisualChild<CheckBox>(cell);
                    if (chkbox != null)
                    {   //Checkbox has been found
                        (chkbox as CheckBox).Focus();
                    }
                    ComboBox combbox = FindVisualChild<ComboBox>(cell);
                    if (combbox != null)
                    {   //ComboBox has been found
                        (combbox as ComboBox).Focus();
                    }
                }
            }
        }

寻找视觉儿童!

public static T FindVisualChild<T>(DependencyObject obj) where T : DependencyObject
{
    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
    {
        DependencyObject child = VisualTreeHelper.GetChild(obj, i);
        if (child != null && child is T)
            return (T)child;
        else
        {
            T childOfChild = FindVisualChild<T>(child);
            if (childOfChild != null)
                return childOfChild;
        }
    }
    return null;
}
创建

派生自DataGrid控件的新控件的简单答案

  using System.Windows.Controls;
   public class CustomDataGrid : DataGrid
   {
    protected override void OnSelectedCellsChanged(SelectedCellsChangedEventArgs e)
    {
        //to make sure cell is selected
        var cells = e.AddedCells.FirstOrDefault();
        if (cells != null)
        {
            this.BeginEdit();
        }
        base.OnSelectedCellsChanged(e);
    }
   }

Hisham 的建议对我来说非常有效,但我会改用OnCurrentCellChanged,因为当SelectionUnit CellOrRowHeaderOnSelectedCellsChanged不起作用。在后一种情况下,仅当所选内容移动到另一行中的单元格时,才会触发BeginEdit()。向左或向右踩踏根本不会触发事件。

此外,建议将 DependencyProperty 添加到自定义控件并在触发 BeginEdit() 之前对其进行检查,以防止此行为(如其他 DataGrids,如 XCeed)。但这不是批评家 - 只是我通常做的事情。

    protected override void OnCurrentCellChanged(EventArgs e)
    {
        // Make sure a cell is selected and only enter edit mode
        // if this is the desired behavior 
        if (CurrentCell != null && EditTrigger == EditTriggers.CellsCurrent)
        {
            this.BeginEdit();
        }
        base.OnCurrentCellChanged(e);
    }

最新更新