Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
When I try to load and set items to WPF DataGrid as:
DataGrid.ItemsSource=data;
I have a problem of calling SelectionChanged event for each DataGrid item added, instead of calling it only when DataGrid selection item changed after DataGrid items is loaded

What I have tried:

DataGrid.ItemsSource = Data;

DataGrid.ItemsSource = Data;
Posted
Updated 4-Feb-20 21:30pm

1 solution

Disconnect your data context beforehand, then reattach it afterwards. I don't know what you have attached your DataContext to so here's an example using something called _myUserControl as the target of the DataContext, which we are storing in DataContextSource (this is an example only).
C#
private void ConnectData()
{
  _myUserControl.DataContext = null;
  DataGrid.ItemsSource = Data;
  _myUserControl.DataContext = DataContextSource;
  // Set your selected item if necessary.
}
 
Share this answer
 
Comments
Member 13602078 5-Feb-20 6:42am    
Thank you, but the problem does not solved, SelectionChanged event is called for each item added to DataGrid, I need to call it after loading all DaraGrid elements and when selection is changed
Pete O'Hanlon 5-Feb-20 7:17am    
Are you subscribing to the SelectionChanged event? If you are, just disconnect from the event handler before you associate Data with your ItemsSource. Resubscribe to SelectionChange immediately after DataGrid.ItemsSource = Data;.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900