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 4064550
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T15:52:10+00:00 2026-05-20T15:52:10+00:00

I have a WPF Datagrid binded to some properties in my ViewModel <DataGrid AutoGenerateColumns=False

  • 0

I have a WPF Datagrid binded to some properties in my ViewModel

<DataGrid AutoGenerateColumns="False" Name="dataGrid" SelectionMode="Single"
          ItemsSource="{Binding ItemList}" SelectedItem="{Binding SelectedItem}">
...
</DataGrid>

When my window load and the Datagrid too, I set the SelectedItem and it binds fine but the row isn’t highlighted. The moment I click a row, the row highlight and the problem is resolved.

How can I set/trigger the highlighting of the SelectedItem in the DataGrid on load/initialization ?

EDIT:

It’s actually selected because I have the little selection cell. It’s just the rendering of the Highlighting that does not trigger.

enter image description here

  • 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-20T15:52:11+00:00Added an answer on May 20, 2026 at 3:52 pm

    When using a Model as the DataContext for a WPF Window, the DataGrid’s SelectionChanged event doesn’t get called until after the Window is loaded which is why the row is never highlighted and you only see the first row with the partial highlight. There may be a more elegant way, but here’s a work-around.

    In the Window’s loaded event or the DataGrid’s loaded event, reset the SelectedItem binding:

    public MainWindow()
    {
        InitializeComponent(); 
        this.Loaded += new RoutedEventHandler( OnLoaded );
    }
    
    // could also be placed in the DataGrid's loaded event handler
    private void OnLoaded( object sender, RoutedEventArgs e )
    {
        if( dataGrid != null && Model.SelectedItem != null )
        {
            var selected = Model.SelectedItem;
            Model.SelectedItem = null;
            Model.SelectedItem = selected;
        }
    }
    

    Here’s a complete working sample.

    XAML

    <Window x:Class="WpfDataGridHighlightOnLoad.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:model="clr-namespace:WpfDataGridHighlightOnLoad" 
            Title="MainWindow" Height="350" Width="525">
    
        <Window.DataContext>
            <model:MainWindowModel x:Name="Model" />
        </Window.DataContext>
    
        <Grid>
            <DataGrid AutoGenerateColumns="True" SelectionMode="Single"
                      HorizontalAlignment="Stretch" 
                      Name="dataGrid" 
                      VerticalAlignment="Top"
                      ItemsSource="{Binding ItemList}"
                      SelectedItem="{Binding SelectedItem}">
            </DataGrid>
    
            <Button Content="Cycle Selection" Click="OnCycleClick" 
                    Height="23" 
                    HorizontalAlignment="Right" 
                    Name="button1" 
                    VerticalAlignment="Bottom" Width="125" />
    
            <Button Content="Reset Grid" Click="OnResetClick" 
                    Height="23" 
                    HorizontalAlignment="Left" 
                    Name="button2" 
                    VerticalAlignment="Bottom" Width="125" />
    
        </Grid>
    </Window>
    

    Code Behind

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Windows;
    
    namespace WpfDataGridHighlightOnLoad
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();   
                this.Loaded += new RoutedEventHandler( OnLoaded );
            }
    
            // could also be placed in the DataGrid's loaded event handler
            private void OnLoaded( object sender, RoutedEventArgs e )
            {
                if( dataGrid != null && Model.SelectedItem != null )
                {
                    var selected = Model.SelectedItem;
                    Model.SelectedItem = null;
                    Model.SelectedItem = selected;
                }
            }
    
            private void OnCycleClick( object sender, RoutedEventArgs e )
            {
                int index = Model.ItemList.IndexOf( Model.SelectedItem );
                index = index == Model.ItemList.Count - 1 ? 0 : index + 1;
                Model.SelectedItem = Model.ItemList[index];
            }
    
            private void OnResetClick( object sender, RoutedEventArgs e )
            {
                Model.Reset();
            }
        }
    
        public class MainWindowModel : INotifyPropertyChanged
        {
            public MainWindowModel()
            {
                Reset();
            }
    
            public void Reset()
            {
                ItemList = new List<Person>
                               {
                                   new Person("Joe", 20),
                                   new Person("John", 30),
                                   new Person("Jane", 40),
                                   new Person("Jill", 50),
                                   new Person("Fido", 7),
                               };
    
                SelectedItem = ItemList[2];
            }
    
            private Person _selectedItem;
            public Person SelectedItem
            {
                get { return _selectedItem; }
                set
                {
                    _selectedItem = value;
                    NotifyPropertyChanged( "SelectedItem" );
                }
            }
    
            private List<Person> _itemList;
            public List<Person> ItemList
            {
                get { return _itemList; }
                set
                {
                    _itemList = value;
                    NotifyPropertyChanged( "ItemList" );
                }
            }
    
            #region INotifyPropertyChanged Members
    
            public event PropertyChangedEventHandler PropertyChanged;
    
            private void NotifyPropertyChanged( String info )
            {
                if( PropertyChanged != null )
                {
                    PropertyChanged( this, new PropertyChangedEventArgs( info ) );
                }
            }
    
            #endregion
        }
    
        public class Person
        {
            public string Name { get; set; }
            public int Age { get; set; }
    
            public Person( string name, int age )
            {
                Name = name;
                Age = age;
            }
    
            public override string ToString()
            {
                return Name;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a WPF application in VS 2008 with some web service references. For
I have a WPF ListView that is bound to a BindingList<T>. The binding works
I have WPF ListBox which is bound to a ObservableCollection, when the collection changes,
I have a WPF project and I'm trying to setup a NAnt build script
I have a WPF Window which has a among other controls hosts a Frame.
I have several wpf pages with update/delete/add buttons. I want to display to the
I have a WPF window for editing database information, which is represented using an
I have a WPF control, that has a list of Investors, and in the
I have a WPF app which snaps to screen edges (I just set the
I have a WPF ListView which repeats the data vertically. I cannot figure out

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.