如何在DataGrid页面加载时集中并开始编辑第一行的第一单元格

时间:2018-08-16 03:41:34

标签: c# wpf datagrid

<script> function b64DecodeUnicode(str) { return decodeURIComponent(atob(str).split('').map(function(c) { return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2); }).join('')); } var words = b64DecodeUnicode(base64_encoded_string_goes_here); document.write(words); </script> 自动加载或页面加载时,我想集中精力并开始编辑第一行的第一单元格。我已经尝试了许多代码,但仍未找到解决方案。我已经为UserControl编写了代码:

DataGrid

在构造函数上,我编写了代码。

<UserControl x:Class="Inventory_Control.UserControls.FileTab"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:Inventory_Control.UserControls"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <Grid Background="#FF2C2929">
        <DataGrid  DataGridCell.Selected="DataGrid_GotFocus" SelectionUnit="Cell" 
                   SelectionMode="Single" Name="Datagrid"  AutoGenerateColumns="False" 
                   PreviewKeyDown="Datagrid_PreviewKeyDown" 
                   CurrentCellChanged="Datagrid_CurrentCellChanged">

            <DataGrid.Columns>

                <DataGridTextColumn Header="Code" Width="1*" Binding="{Binding Code}"/>
                <DataGridTextColumn Header="Code" Width="1*" Binding="{Binding Name}"/>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</UserControl>

1 个答案:

答案 0 :(得分:1)

您应该获得要关注的单元格的参考。等待直到元素已加载,然后使用VisualTreeHelper类:

public FileTab()
{
    InitializeComponent();
    AddValues();

    Loaded += (s, e) =>
    {
        DataGrid grid = dg;
        DataGridRow rowContainer = grid.ItemContainerGenerator.ContainerFromIndex(0) as DataGridRow;
        if (rowContainer != null)
        {
            DataGridCellsPresenter presenter = FindVisualChild<DataGridCellsPresenter>(rowContainer);
            if (presenter != null)
            {
                DataGridCell cell = presenter.ItemContainerGenerator.ContainerFromIndex(0) as DataGridCell;
                if (cell != null)
                {
                    DataGridCellInfo dataGridCellInfo = new DataGridCellInfo(cell);
                    if (!grid.SelectedCells.Contains(dataGridCellInfo))
                    {
                        grid.SelectedCells.Add(dataGridCellInfo);
                    }
                    grid.CurrentCell = dataGridCellInfo;
                    grid.BeginEdit();
                }
            }
        }
    };
}

private 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;
}