Zohaib
Zohaib

Reputation: 199

How to focus and begin edit first cell of first row on page load of DataGrid

I want to focus and begin edit first cell of first row when UserControl load or page load automatically. I've tried many code but wont find solution yet.I've write code for 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>

and on Constructor i've write code.

public FileTab()
{
    InitializeComponent();

    AddValues();


    var grid = Datagrid;
    var cell = new DataGridCellInfo(0, grid.Columns[0]);
    grid.CurrentCell = cell;
    Dispatcher.BeginInvoke(new Action(() =>
    {
        grid.BeginEdit();
    }), System.Windows.Threading.DispatcherPriority.Background);

}

Upvotes: 0

Views: 191

Answers (1)

mm8
mm8

Reputation: 169220

You should get a reference to the cell to be focused. Wait until the elements have been loaded and then use the VisualTreeHelper class:

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

Upvotes: 1

Related Questions