How To Prevent WPF DataGrid From De-Selecting SelectedItem When Items Updated?

"YOU AND THE ART OF ONLINE DATING" is the only product on the market that will take you step-by-step through the process of online dating, provide you with the resources to help ensure success. Get it now!

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"> /// Interaction logic for MainWindow. Xaml /// public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); this. DataContext = new ViewModel(); } } public class ViewModel { private DataGenerator dataGenerator; private ObservableCollection modelCollection; public ListCollectionView ModelCollectionView { get; private set; } public ViewModel() { modelCollection = new ObservableCollection(); ModelCollectionView = new ListCollectionView(modelCollection); //Create models for (int I = 0; I Replace /// Interaction logic for MainWindow.

Xaml /// public partial class MainWindow : Window { ObservableCollection items; ListCollectionView lcv; public MainWindow() { InitializeComponent(); items = new ObservableCollection(); 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. Itemsindex = new MyClass() { Name = "NewItem" + ctr++.ToString() }; lcv. MoveCurrentToPosition(index); } } }.

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 Code-Behind: using System; using System. Windows; using System.Windows.

Data; using System.Collections. ObjectModel; using System.Windows. Threading; namespace ContextTest { /// /// Interaction logic for MainWindow.

Xaml /// public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); this. DataContext = new ViewModel(); } } public class ViewModel { private DataGenerator dataGenerator; private ObservableCollection modelCollection; public ListCollectionView ModelCollectionView { get; private set; } public ViewModel() { modelCollection = new ObservableCollection(); ModelCollectionView = new ListCollectionView(modelCollection); //Create models for (int I = 0; I Replace 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 { /// /// Interaction logic for MainWindow. Xaml /// public partial class MainWindow : Window { ObservableCollection items; ListCollectionView lcv; public MainWindow() { InitializeComponent(); items = new ObservableCollection(); 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. Itemsindex = new MyClass() { Name = "NewItem" + ctr++.ToString() }; lcv.

MoveCurrentToPosition(index); } } }.

Doesn't really work for me KarmicPuppet. Problem is I am doing MVVM and that makes it a slightly different problem. I can't just handle everything in a button click handler.

– Chris Holmes Sep 2 '10 at 18:51 I'm not suggesting that you handle everything in a button click handler. That was just an example code. The idea is that you do something in your ViewModel that is similar to the one in the Button_Click event in my example.

Let me try to write you an MVVM example. I'll get back to you. – karmicpuppet Sep 2 '10 at 19:53 See Edit.

I hope this gives you a better idea. It contains a ListBox whose data is being randomly updated every 5 seconds. You'll see that it still keeps the selection after the updates.

– karmicpuppet Sep 2 '10 at 20:27.

I haven't worked with the WPF DataGrid, but I'd try this approach: Add a property to the view-model that will hold the value of the currently selected item. Bind SelectedItem to this new property using TwoWay. This way, when the user selects a row, it will update the view-model, and when the ObservableCollection gets updated it won't affect the property to which SelectedItem is bound.

Being bound, I wouldn't expect it could reset in the way you're seeing.

That was my first thought Jay, but so far no dice. It's still resetting it. – Chris Holmes Sep 2 '10 at 16:15 @Chris Is the selected item itself being updated, or just other items?

– Jay Sep 2 '10 at 16:32 All items are being updated. – Chris Holmes Sep 2 '10 at 17:44.

You could, in the logic that updates the Collection, save off the CollectionView. Current item reference to another variable. Then, after you're done updating, call CollectionView.

MoveCurrentTo(variable) to reset the selected item.

MyClass selectedItem = this.lb. Int index = this.items.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions