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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:26:24+00:00 2026-05-16T15:26:24+00:00

My scenario: I have a background thread that polls for changes and periodically updates

  • 0

My scenario: I have a background thread that polls for changes and periodically updates a WPF DataGrid’s ObservableCollection (MVVM-style). The user can click on a row in the DataGrid and bring up the “details” of that row in an adjacent UserControl on the same main view.

When the background thread has updates, it cycles through the objects in the ObservableCollection and replaces individual objects if they have changed (in other words, I am not rebinding a whole new ObservableCollection to the DataGrid, but instead replacing individual items in the collection; this allows the DataGrid to maintain sorting order during updates).

The problem is that after a user has selected a specific row and the details are displayed in the adjacent UserControl, when the background thread updates the DataGrid the DataGrid loses the SelectedItem (it gets reset back to index of -1).

How can I retain the SelectedItem between updates to the ObservableCollection?

  • 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-16T15:26:25+00:00Added an answer on May 16, 2026 at 3:26 pm

    If your grid is single-selection, my suggestion is that you use the CollectionView as the ItemsSource instead of the actual ObservableCollection. Then, make sure that Datagrid.IsSynchronizedWithCurrentItem is set to true. Finally, at the end of your “replace item logic”, just move the CollectionView’s CurrentItem to the corresponding new item.

    Below is a sample that demonstrates this. (I’m using a ListBox here though. Hope it works fine with your Datagrid).

    EDIT – NEW SAMPLE USING MVVM:

    XAML

    <Window x:Class="ContextTest.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            x:Name="window"
            Title="MainWindow" Height="350" Width="525">
        <DockPanel>
            <ListBox x:Name="lb" DockPanel.Dock="Left" Width="200" 
                     ItemsSource="{Binding ModelCollectionView}"
                     SelectionMode="Single" IsSynchronizedWithCurrentItem="True">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Path=Name}"/>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
    
            <TextBlock Text="{Binding ElementName=lb, Path=SelectedItem.Description}"/>
    
        </DockPanel>
    </Window>
    

    Code-Behind:

    using System;
    using System.Windows;
    using System.Windows.Data;
    using System.Collections.ObjectModel;
    using System.Windows.Threading;
    
    namespace ContextTest
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                this.DataContext = new ViewModel();
            }
        }
    
        public class ViewModel
        {
            private DataGenerator dataGenerator;
            private ObservableCollection<Model> modelCollection;
            public ListCollectionView ModelCollectionView { get; private set; }
    
            public ViewModel()
            {
                modelCollection = new ObservableCollection<Model>();
                ModelCollectionView = new ListCollectionView(modelCollection);
    
                //Create models
                for (int i = 0; i < 20; i++)
                    modelCollection.Add(new Model() { Name = "Model" + i.ToString(), 
                        Description = "Description for Model" + i.ToString() });
    
                this.dataGenerator = new DataGenerator(this);
            }
    
            public void Replace(Model oldModel, Model newModel)
            {
                int curIndex = ModelCollectionView.CurrentPosition;
                int n = modelCollection.IndexOf(oldModel);
                this.modelCollection[n] = newModel;
                ModelCollectionView.MoveCurrentToPosition(curIndex);
            }
        }
    
        public class Model
        {
            public string Name { get; set; }
            public string Description { get; set; }
        }
    
        public class DataGenerator
        {
            private ViewModel vm;
            private DispatcherTimer timer;
            int ctr = 0;
    
            public DataGenerator(ViewModel vm)
            {
                this.vm = vm;
                timer = new DispatcherTimer(TimeSpan.FromSeconds(5), 
                    DispatcherPriority.Normal, OnTimerTick, Dispatcher.CurrentDispatcher);
            }
    
            public void OnTimerTick(object sender, EventArgs e)
            {
                Random r = new Random();
    
                //Update several Model items in the ViewModel
                int times = r.Next(vm.ModelCollectionView.Count - 1);
                for (int i = 0; i < times; i++)
                {   
                    Model newModel = new Model() 
                        { 
                            Name = "NewModel" + ctr.ToString(),
                            Description = "Description for NewModel" + ctr.ToString()
                        };
                    ctr++;
    
                    //Replace a random item in VM with a new one.
                    int n = r.Next(times);
                    vm.Replace(vm.ModelCollectionView.GetItemAt(n) as Model, newModel);
                }
            }
        }
    }
    

    OLD SAMPLE:

    XAML:

    <Window x:Class="ContextTest.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>
            <ListBox x:Name="lb" SelectionMode="Single" IsSynchronizedWithCurrentItem="True" SelectionMode="Multiple">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Path=Name}"/>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
    
            <TextBlock Text="{Binding ElementName=lb, Path=SelectedItem.Name}"/>
            <Button Click="Button_Click">Replace</Button>
    
    
        </StackPanel>
    </Window>
    

    Code-behind:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    using System.Collections.ObjectModel;
    using System.ComponentModel;
    
    namespace ContextTest
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            ObservableCollection<MyClass> items;
            ListCollectionView lcv;
    
            public MainWindow()
            {
                InitializeComponent();
    
                items = new ObservableCollection<MyClass>();
                lcv = (ListCollectionView)CollectionViewSource.GetDefaultView(items);
                this.lb.ItemsSource = lcv;
                items.Add(new MyClass() { Name = "A" });
                items.Add(new MyClass() { Name = "B" });
                items.Add(new MyClass() { Name = "C" });
                items.Add(new MyClass() { Name = "D" });
                items.Add(new MyClass() { Name = "E" });
    
            }
    
            public class MyClass
            {
                public string Name { get; set; }
            }
    
            int ctr = 0;
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                MyClass selectedItem = this.lb.SelectedItem as MyClass;
                int index = this.items.IndexOf(selectedItem);
                this.items[index] = new MyClass() { Name = "NewItem" + ctr++.ToString() };
                lcv.MoveCurrentToPosition(index);
            }
    
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.