Dear Vijayarasan,
Sample link: https://www.syncfusion.com/downloads/support/forum/161511/ze/Sample-1254939473
in the xaml this line yells at me:
local:SfDataGridHelper.SelectedItems="{Binding SelectedItems, Mode=TwoWay}">
And running it I get:
System.NullReferenceException
for this section:
sfDataGrid.SelectedItems.CollectionChanged += (sender, e) =>
{
SfDataGridHelper.SetSelectedItems(sfDataGrid, sfDataGrid.SelectedItems);
};
Do you have any views on this?
frank
Frank,
We have recently made some changes to the DataGrid Selection. For more
information on this change, please refer to the release notes linked below.
Release Notes Link: https://help.syncfusion.com/wpf/release-notes/v20.3.0.49?type=all#sfdatagrid-bug-fixes
We only initialize the SelectedItems collection after the control is
initialized to provide the proper binding support for the collection. In your scenario,
you are trying to access the SelectedItems before the control is loaded, which
is why the reported problem occurs.
To overcome this issue, we suggest that you either initialize the SelectedItems
before accessing it or use the Dispatcher. Refer to the code snippet below.
private static void OnSelectedItemsChanged(DependencyObject d, DependencyPropertyChangedEventArgs args) { var sfDataGrid = d as SfDataGrid; if (sfDataGrid == null) return; //SfDataGridHelper.SelectedItems property updated based on SfDataGrid.SelectedItems Collectionchanged event.
////Solution 1:Here initialize the SelectedItems before accessing //if(sfDataGrid.SelectedItems == null) //{ // sfDataGrid.SelectedItems = new ObservableCollection<object>(); //}
//sfDataGrid.SelectedItems.CollectionChanged += (sender, e) => //{ // SfDataGridHelper.SetSelectedItems(sfDataGrid, sfDataGrid.SelectedItems); //};
//Solution 2: Here using Dispatcher sfDataGrid.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.ApplicationIdle, new Action(() => { //This event call after control loaded sfDataGrid.SelectedItems.CollectionChanged += (sender, e) => { SfDataGridHelper.SetSelectedItems(sfDataGrid, sfDataGrid.SelectedItems); };
})); } |
Find the modified sample in the attachment.
Regards,
Vijayarasan S
If this post is helpful, please consider Accepting
it as the solution so that other members can locate it more quickly.