Xaphann
Xaphann

Reputation: 3677

WPF DataGrid changing row selection to a cell's TextBox

I have a DataGrid and when the users hits the down arrow it need to move to next row below but focus on a cell that has a textbox in in. I want to not focus on textbox not the actual cell. This is what my DataGrid looks like;

<DataGrid Name="myDataGrid" PreviewKeyDown="myDataGrid_PreviewKeyDown">
<DataGrid.Columns>
    <DataGridTemplateColumn Header="Description" IsReadOnly="True" Width="Auto">
        <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                    <TextBlock HorizontalAlignment="Right" Text="{Binding Path=Descrip, Mode=TwoWay, NotifyOnTargetUpdated=True, UpdateSourceTrigger=PropertyChanged, StringFormat=C, ConverterCulture=en-us}" TextAlignment="Right"/>
            </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
    <DataGridTemplateColumn Header="Units Counted" Width="Auto">
        <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <TextBox BorderBrush="Black" BorderThickness="2" FontWeight="Bold" Margin="2" Text="{Binding Path=UnitCounted, Mode=TwoWay, NotifyOnTargetUpdated=True, UpdateSourceTrigger=PropertyChanged}" TextAlignment="Right" />
            </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
</DataGrid.Columns>

private void myDataGrid_PreviewKeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Down)
        {
            e.Handled = true;
            int indx = myDataGrid.SelectedIndex + 1;
            myDataGrid.SelectedIndex = indx;
            myDataGrid.CurrentCell = new DataGridCellInfo(myDataGrid.Items[indx], myDataGrid.Columns[1])
        }
    }

Doing this selects the cell not the textbox. If I add this to textbox;

FocusManager.FocusedElement="{Binding RelativeSource={RelativeSource Self}}"

It adds a cursor to every row textbox but doesn't focus on the textbox.

thanks

edit: Thanks to @Sheridan I am closer with this code;

<TextBox 
BorderBrush="Black" 
BorderThickness="2" 
FontWeight="Bold" 
Margin="2" 
Text="{Binding Path=UnitCounted, Mode=TwoWay, NotifyOnTargetUpdated=True, UpdateSourceTrigger=PropertyChanged}" 
TextAlignment="Right" 
FocusManager.IsFocusScope="True"
FocusManager.FocusedElement="{Binding RelativeSource={RelativeSource Self}}"/>

Then in my myDataGrid_PreviewKeyDown adding in myDataGrid.BeginEdit() allows for it to work. The problem is that it seems that BeginEdit never actually ends. How do I fix this?

Upvotes: 0

Views: 2331

Answers (1)

123 456 789 0
123 456 789 0

Reputation: 10865

I wouldn't mess with the FocusManager.FocusedElement as you get unknown caveats that it can do like what you are experiencing right now with the BeginEdit.

What I did to focus on the TextBox is simple, in xaml

<DataGrid Name="myDataGrid" AutoGenerateColumns="False" ItemsSource="{Binding Loans}" PreviewKeyDown="UIElement_OnPreviewMouseDown">
    <DataGrid.Columns>
        <DataGridTemplateColumn Header="Units Counted" Width="Auto">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBox x:Name="sampleTextBox" HorizontalAlignment="Right" Text="{Binding Name}"/>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

And in class file I have

    public static T GetVisualChild<T>(Visual parent) where T : Visual
    {
        T child = default(T);
        int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
        for (int i = 0; i < numVisuals; i++)
        {
            Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
            child = v as T;
            if (child == null)
            {
                child = GetVisualChild<T>(v);
            }
            if (child != null)
            {
                break;
            }
        }
        return child;
    }

    private void UIElement_OnPreviewMouseDown(object sender, KeyEventArgs e)
    {
        if (e.Key != Key.Down) return;
        e.Handled = true;
        var indx = myDataGrid.SelectedIndex + 1;
        myDataGrid.SelectedIndex = indx;
        myDataGrid.CurrentCell = new DataGridCellInfo(myDataGrid.Items[indx], myDataGrid.Columns[0]);

        var row = this.myDataGrid.ItemContainerGenerator.ContainerFromItem(myDataGrid.CurrentCell.Item) as DataGridRow;
        var presenter = GetVisualChild<DataGridCellsPresenter>(row);


        var cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromItem(myDataGrid.CurrentCell.Item);
        if (cell != null)
        {
            var contentPresenter = cell.Content as ContentPresenter;
            if (contentPresenter != null)
            {
                var m = contentPresenter.ContentTemplate.FindName("sampleTextBox", contentPresenter);
                ((TextBox) m).Focus();
            }
        }
    }
}

Now that'll make the TextBox be focus when Down key is pressed every time and changes the cell selection.

Upvotes: 1

Related Questions