Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7013097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:20:32+00:00 2026-05-27T22:20:32+00:00

When I tab into the WPF Datagrid it focuses the first cell (with a

  • 0

When I tab into the WPF Datagrid it focuses the first cell (with a rectangle) but does not select it (in blue). If I press tab again it focuses and selects it.

I think the DataGridCell actually has IsSelected=true, but it is not being painted in blue. I have tried hacking around with the datagrid and visual-states but I can’t make it repaint the grid correctly when you first tab in.

Has anyone seen this before and do you have a solution?

code to reproduce:

MainWindow.xaml

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
    <StackPanel>
        <TextBox Width="100"/>
        <DataGrid SelectionMode="Single" SelectionUnit="Cell"
              ItemsSource="{Binding MyItems}" AutoGenerateColumns="True"/>
    </StackPanel>
</Window>

MainWindow.xaml.cs

using System.Collections.Generic;
using System.Windows;

namespace WpfApplication1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            MyItems.Add(new Thingy() { Name = "Frank", Age = 34 });
            MyItems.Add(new Thingy() { Name = "Jim", Age = 43 });
            MyItems.Add(new Thingy() { Name = "Bob", Age = 56 });
            MyItems.Add(new Thingy() { Name = "Harry", Age = 23 });

            DataContext = this;
        }

        private List<Thingy> _myItems = new List<Thingy>();
        public List<Thingy> MyItems
        {
            get { return _myItems; }
        }
    }

    public class Thingy
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }
}

click on the TextBox, then hit tab — cell 1 is not selected

hit tab again — cell 2 is selected

Any help is much appreciated, thanks.

Update:

When SelectionUnit=FullRow, I have had some success along the lines shown below, if SelectedIndex is set to 0 upon creation the first row is now selected in blue. It still needs some work to cope with shift-tab etc. There is still a problem though because when I change the SelectionMode to extended and press shift-downarrow the second row gets selected but the first row gets unselected (they should both be selected). If I do it again rows 2+3 are selected which is correct and it continues to work ok after that.

protected override void OnIsKeyboardFocusWithinChanged(DependencyPropertyChangedEventArgs e)
    {
        base.OnIsKeyboardFocusWithinChanged(e);

        int oldIdx = this.SelectedIndex;
        this.SelectedIndex = -1;
        this.SelectedIndex = oldIdx;
    }

Further Update:

Fixed that issue by setting the private _selectionAnchor field. (Thanks ILSpy)

 protected override void OnIsKeyboardFocusWithinChanged(DependencyPropertyChangedEventArgs e)
    {
        base.OnIsKeyboardFocusWithinChanged(e);

        this.SelectedIndex = -1;
        this.SelectedIndex = 0;

        SelectionAnchor = SelectedCells[0];
    }

    protected DataGridCellInfo? SelectionAnchor
    {
        get
        {
            return typeof(DataGrid).GetField("_selectionAnchor", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(this) as DataGridCellInfo?;
        }
        set
        {
            typeof(DataGrid).GetField("_selectionAnchor", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(this, value);
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T22:20:32+00:00Added an answer on May 27, 2026 at 10:20 pm

    You can do like this. Register for a got focus event and then set the original source as selected item.

    <Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
    <StackPanel>
        <TextBox Width="100"/>
        <DataGrid SelectionMode="Single" SelectionUnit="Cell"
              ItemsSource="{Binding MyItems}" AutoGenerateColumns="True"  
                GotFocus="WPF_DataGrid_GotFocus" />
    </StackPanel>
    </Window>
    

    Then in the code behind file :

        private void WPF_DataGrid_GotFocus(object sender, RoutedEventArgs e)
        {
            (e.OriginalSource as DataGridCell).IsSelected = true;
    
        }
    

    I hope it helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to export an Excel database into .txt (Tab Delimited), but some of
How can I add WPF form into tab item of tab control? like I
Xcode what is the key to tab into the next auto suggested parameter block?
I currently have an extension Method which converts an IEnumerable of type Tab into
I needed to convert a Tab Seperated Text file into a tabular format as
When I Alt + Tab to my VM from my host the VM does
I am using a WPF ribbon and I have just run into some strange
Demo: http://jsfiddle.net/55ucw/1/ In any browser other than Safari or Chrome, I can tab into
I am trying to work out how to insert a tab character into a
I am migrating some win forms panels to WPF. In WinForms, you can tab

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.