SelectedItems was loaded but lines not checked

Hi,

I have been stuck on a problem for a few hours and I no longer understand it.

Context:
I have a MainWindow with two components, an sfTreeView and an sfTreeGrid.
(I am attaching an image to illustrate)
Selecting a node on the TreeView refreshes the TreeGrid.

The DataContext (ViewModel) of MainWindow has the following properties:
public object SelectedModel
{
            get => selectedModel; //Was declarated ;)
            set
            {
                selectedModel= value;
                RaisePropertyChanged("SelectedModel");
                RaisePropertyChanged("GridViewModel");
            }
 }
public GridAttributViewModel GridViewModel
{
            get
            {
                return new GridAttributViewModel((CModel)SelectedModel);
            }
 }


The GridAttributViewModel has the following properties:
public CModel SelectedModel  
{
            get => SelectedModel  ;
            set
            {
                SelectedModel  = value;
                RaisePropertyChanged("SelectedModel  ");
                RaisePropertyChanged("ListInfosModel");
                RaisePropertyChanged("SelectedInfos");
            }
}
public ObservableCollection ListInfosModel
{
            get
            {
                if (SelectedModel  == null)
                    return new ObservableCollection();

                if (SelectedModel.ListInfosModel.Count == 0)
                    SelectedModel.ListInfosModel = new ObservableCollection([GETFROMBASE]); //Load on demand

                return SelectedClass.ListInfosModel;
            }
}

public ObservableCollection SelectedInfos
{
            get
            {
                if (SelectedModel  == null)
                    return new ObservableCollection();

                return SelectedModel.SelectedInfos;
            }
}
       
 public GridAttributViewModel(CModel model)
{
            SelectedModel  = model;
}

The CModel class (This is an example) has the following properties:

public ObservableCollection ListInfosModel { get; set; } = new ObservableCollection();
public ObservableCollection SelectedInfos { get;set; } = new ObservableCollection();



When I select a node in the sfTreeView, the TreeGrid needs to be updated:
For this I create a GridViewModel based on CModel like declarated behind and

TreeGrid.ItemsSource is ok
TreeGrid.SelectedItems looks ok (there are the objects in it) but is no selected in UI -> that's my problem, do you have an idea?
Strange thing when I select a line it is added to the SelectedInfos of the CModel and it is checked on UI!

SfTreeView declaration :

            AutoExpandMode="AllNodes"
            ShowLines="True"
            BorderBrush="LightGray"
            BorderThickness="1"
            CheckBoxMode="Recursive"
            CheckedItems="{Binding Path=CheckedModels, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
            ChildPropertyName="Models"
            ExpandActionTrigger="Node"
            FocusVisualStyle="{x:Null}"
            IsAnimationEnabled="True"
            ItemTemplateDataContextType="Node"
            ItemsSource="{Binding ListXXXX}"
            SelectionMode="Single"
            SelectedItem="{Binding Path=SelectedModel, Mode=TwoWay}"
            NodePopulationMode="Instant">

SfDataGrid Declaration : 
                                    ChildPropertyName=""
                                    DataContext="{Binding GridViewModel}"
                                    AutoGenerateColumns="False" ColumnSizer="Star" NavigationMode="Row"
                                    ItemsSource="{Binding ListInfosModel}"
                                   SelectedItems="{Binding SelectedInfos, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                    SelfRelationRootValue="-1"  SelectionMode="Multiple"
                                    ShowCheckBox="True" CheckBoxSelectionMode="SynchronizeSelection" EnableRecursiveChecking="False">


Thanks for your help




16 Replies

GE Geoffrey March 2, 2021 02:01 PM UTC

I also notice that when I change the DataContext (GridAttributViewModel) of my sfTreeGrid (By selecting another node in the sfTreeView) my "SelectedInfos" property is totally emptied :(


VS Vijayarasan Sivanandham Syncfusion Team March 3, 2021 05:12 PM UTC

Hi Geoffrey,

Thank you for contacting Syncfusion support.

We are little unclear with your scenario. Can you please provide the more information related to your query?

Can you please share us below things?
       
        1. We suspect that in your application SfTreeView and SfTreeGrid have binded with different ItemSource collections. In this case, SelectedItem in SfTreeView If the collection contains the same Item in SfTreeGrid that item will be selected in SfTreeGrid if this is your scenario
        2. If SfTreeView and SfTreeGrid have binded with same ItemSource collections. In this case, SfTreeView SelectedItem that Item will be Selected in SfTreeGrid if this is your scenario
        3.
Provide more details about your scenario with illustrations? 
If we misunderstood your requirement, please provide more information regarding the requirement. This would help us to proceed further. 
Regards,
Vijayarasan S
 



GE Geoffrey March 3, 2021 06:26 PM UTC

Hi,

To simplify the scenario I only have one DataContext for the Window.

I will try to be clearer.

I have two classes CModel and CInfosModel

  • CModel has two ObservableCollections:
One having the total list of CInfosModel: ObservableCollection<CInfosModel> ListInfosModel

One having the list of CInfosModel selected: ObservableCollection<CInfosModel> SelectedInfos


The sfTreeView is used to select a CModel

By selecting CModel I want in the sfTreeGrid to appear the ObservableCollection <CInfosModel> ListInfosModel of the CModel
==> This works fine

and that the CInfosModel present in ObservableCollection <CInfosModel> SelectedInfos are automatically checked.
==>This part does not work 

For this I created a ViewModel with this properties:
 - "SelectedModel" bound to the "SelectedItem" of the sfTreeView allows you to know the selected CModel, 
The set triggering the RaisePropertyChange of the two following properties:

- "ListInfosModel" bound to the "ItemsSource" of the sfTreeGrid, the get returns "SelectedModel.ListInfosModel"

- "ListSelectedInfos" bound to the "SelectedItems" of the sfTreeGrid, the get returns "SelectedModel.SelectedInfo"

I notice several things:
- Changing the "ItemsSource" on the SfTreeGrid (For example after selecting another CModel in the sfTreeView) clears the list of "SelectedItems" :(

- The "SelectedItems" seems filled, but the lines are not checked, on the other hand if I check a line it seems to be added to the ObservableCollection<CInfosModel> SelectedInfos, which can cause a duplicate


A question that could help me:
How is the link created between the objects of "ItemsSource" and "SelectItems"?

I hope I have been clearer.

If necessary I will try to make a SandBox.

Thx
Geoffrey


VS Vijayarasan Sivanandham Syncfusion Team March 4, 2021 09:11 PM UTC

Hi Geoffrey

Thanks foe the update.

Can you please confirm in your application SfTreeView contained data is same or different data 
 in SfTreeGrid.  
It will be helpful for us to check on it and provide you the solution at the earliest.  
Regards,
Vijayarasan S
 



GE Geoffrey March 5, 2021 09:55 AM UTC

Hi,

The data between the sfTreeView and the sfTreeGrid is different but related as the following image shows.

The sfTreeView is bound on an ObservableCollection

The ItemsSource of the sfTreeGrid is bound to a property of the CModel selected in the sfTreeView:
The property is: ObservableCollection ListInfosModel

The SelectedItems of the sfTreeGrid is bound to a property of the CModel selected in the sfTreeView:
The property is: ObservableCollection SelectedInfos

The objects found in ObservableCollection SelectedInfos are the CInfosModel checked in the sfTreeGrid




GE Geoffrey March 5, 2021 09:57 AM UTC

Hi,

I made you a sandbox that will make it easier to understand.
You will be able to see the problems.

thx,
Geoffrey

Attachment: SyncfusionWpfApp1_69af1c7.zip


VS Vijayarasan Sivanandham Syncfusion Team March 8, 2021 06:47 PM UTC

Hi Geoffrey,

Thanks for the update.
 
We configured your sample at our end and we execute the sample properly. Currently, we are checking your provided sample from our end. We will validate and update you details on March 10, 2021. 
We appreciate your patience until then.

Regards,
Vijayarasan S
 



VS Vijayarasan Sivanandham Syncfusion Team March 10, 2021 06:16 PM UTC

Hi Geoffrey,

 
Sorry for the inconvenience.    
 
We are still working on this. We will update with further details on or before March 12, 2021. We appreciate your patience and understanding. 
 
Regards, 
Vijayarasan S 



VS Vijayarasan Sivanandham Syncfusion Team March 12, 2021 06:54 PM UTC

Hi Geoffrey,

 
Sorry for the inconvenience.    
 
We are still working on this. We will update with further details on or before March 16, 2021. We appreciate your patience and understanding. 
 
Regards, 
Vijayarasan S 



GE Geoffrey March 12, 2021 08:13 PM UTC

Hi,

I understand but I didn't expect this to give you so much trouble.

Hoping to have a quick response because my problem is pending.

thx


VS Vijayarasan Sivanandham Syncfusion Team March 15, 2021 05:52 PM UTC

Hi Geoffrey,

Thanks for the update.

 
As we mentioned earlier, we are checking your code snippet from our end. We will validate and update you further details on March 16, 2020. 
 
We appreciate your patience until then. 
 
Regards, 
Vijayarasan S 



VS Vijayarasan Sivanandham Syncfusion Team March 16, 2021 05:45 PM UTC

Hi Geoffrey,

Thank you for your patience.

Based on provided information we have checked your provided sample. In this sample while selecting the SfTreeView item-based node loaded in SfTreeGrid. In this case, SfTreeGrid loaded node not checked this is your reported scenario.

Your requirement of SfTreeGrid loaded item checkbox state should be checked and need not selected. You can bind state of node checkbox to the bool property in underlying data object by using SfTreeGrid.CheckBoxMappingName property. TreeGrid updates the checked state of checkbox when underlying data object property gets changed and vice versa.

UG Link: https://help.syncfusion.com/wpf/treegrid/node-checkbox#saving-and-loading-node-checkbox-state-from-the-property-in-data-object

If you want to synchronize the selection with node CheckBox’s IsChecked state, you need to set SfTreeGrid.CheckBoxSelectionMode as SynchronizeSelection. In this mode, you can select by checking checkbox and selecting/deselecting the row will check/uncheck the corresponding node checkbox. For more reference please refer the below user guide documentation,

UG Link: https://help.syncfusion.com/wpf/treegrid/node-checkbox#handling-selection-based-on-checkbox-state

If we misunderstood your requirement, please provide more information regarding the requirement. This would help us to proceed further.

Regards,
Vijayarasan S
 



GE Geoffrey March 17, 2021 01:13 PM UTC

Hello,

It looks like your solution might be what I want but when I add
"CheckBoxMappingName" I have an error when launching:
"Object reference not set to an instance of an object."

Here is the stack:

   at Syncfusion.UI.Xaml.TreeGrid.NodeCheckBoxController.CheckPropertyNameInItemproperties()
   at Syncfusion.UI.Xaml.TreeGrid.NodeCheckBoxController.ValidateCheckBoxPropertyName()
   at Syncfusion.UI.Xaml.TreeGrid.SfTreeGrid.CreateTreeGridCollectionView(Object itemsSource)
   at Syncfusion.UI.Xaml.TreeGrid.SfTreeGrid.SetSourceList(Object itemsSource)
   at Syncfusion.UI.Xaml.TreeGrid.SfTreeGrid.RefreshPanelAndView()
   at Syncfusion.UI.Xaml.TreeGrid.SfTreeGrid.OnApplyTemplate()
   at System.Windows.FrameworkElement.ApplyTemplate()
   at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
   at System.Windows.UIElement.Measure(Size availableSize)
   at System.Windows.Controls.Grid.MeasureCell(Int32 cell, Boolean forceInfinityV)
   at System.Windows.Controls.Grid.MeasureCellsGroup(Int32 cellsHead, Size referenceSize, Boolean ignoreDesiredSizeU, Boolean forceInfinityV, Boolean& hasDesiredSizeUChanged)
   at System.Windows.Controls.Grid.MeasureOverride(Size constraint)
   at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
   at System.Windows.UIElement.Measure(Size availableSize)
   at MS.Internal.Helper.MeasureElementWithSingleChild(UIElement element, Size constraint)
   at System.Windows.Controls.ContentPresenter.MeasureOverride(Size constraint)
   at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
   at System.Windows.UIElement.Measure(Size availableSize)
   at System.Windows.Controls.Decorator.MeasureOverride(Size constraint)
   at System.Windows.Documents.AdornerDecorator.MeasureOverride(Size constraint)
   at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
   at System.Windows.UIElement.Measure(Size availableSize)
   at System.Windows.Controls.Border.MeasureOverride(Size constraint)
   at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
   at System.Windows.UIElement.Measure(Size availableSize)
   at System.Windows.Window.MeasureOverrideHelper(Size constraint)
   at System.Windows.Window.MeasureOverride(Size availableSize)
   at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
   at System.Windows.UIElement.Measure(Size availableSize)
   at System.Windows.Interop.HwndSource.SetLayoutSize()
   at System.Windows.Interop.HwndSource.set_RootVisualInternal(Visual value)
   at System.Windows.Interop.HwndSource.set_RootVisual(Visual value)
   at System.Windows.Window.SetRootVisual()
   at System.Windows.Window.SetRootVisualAndUpdateSTC()
   at System.Windows.Window.SetupInitialState(Double requestedTop, Double requestedLeft, Double requestedWidth, Double requestedHeight)
   at System.Windows.Window.CreateSourceWindow(Boolean duringShow)
   at System.Windows.Window.CreateSourceWindowDuringShow()
   at System.Windows.Window.SafeCreateWindowDuringShow()
   at System.Windows.Window.ShowHelper(Object booleanBox)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
   at System.Windows.Threading.DispatcherOperation.InvokeImpl()
   at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state)
   at MS.Internal.CulturePreservingExecutionContext.CallbackWrapper(Object obj)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at MS.Internal.CulturePreservingExecutionContext.Run(CulturePreservingExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Windows.Threading.DispatcherOperation.Invoke()
   at System.Windows.Threading.Dispatcher.ProcessQueue()
   at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
   at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
   at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
   at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
   at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
   at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
   at System.Windows.Threading.Dispatcher.Run()
   at System.Windows.Application.RunDispatcher(Object ignore)
   at System.Windows.Application.RunInternal(Window window)
   at System.Windows.Application.Run()
   at SyncfusionWpfApp1.App.Main()


I tried :
- CheckBoxMappingName = "IsSelected" which causes the error
- CheckBoxMappingName = "IsChecked" which causes the error
- CheckBoxMappingName = "{Binding Path = IsSelected, Mode = TwoWay}" I no longer have an error but the property is never updated

I updated all Syncfusion packages to 18.4.0.48.
I attach you the modified project.
Can you give me a working example please?

Attachment: SyncfusionWpfApp1_d4c790ea.zip


VS Vijayarasan Sivanandham Syncfusion Team March 18, 2021 01:59 PM UTC

Hi Geoffrey,

Thanks for the update.

We have checked the reported issue from our end. Reported issue occurs when the collection contains no items in SfTreeGrid.

We have created bug report for the reported issue “NullReferenceException occurs when the underlying collection has zero items and CheckBoxMappingName property is set in SfTreeGrid”. You are using NuGet package version and we did not provide patch for NuGet version. So, we will fix this issue and include in our first NuGet release on after the Essential Studio 2021 Volume 1 Main release.

You can track the status of this report through the following feedback link,

Feedback link:  https://www.syncfusion.com/feedback/23205/nullreferenceexception-occurs-when-the-underlying-collection-has-zero-items-and
 
Note: The provider feedback link is private, and you need to login to view this feedback.

We appreciate your patience until then.

Regards,
Vijayarasan S
 



VS Vijayarasan Sivanandham Syncfusion Team March 31, 2021 05:45 PM UTC

Hi Geoffrey,

Thank you for your patience.
  
As we mentioned earlier, We will fix this issue and include in our April 06th ,2021 NuGet release.

You can track the status of this report through the following feedback link,

Feedback Link: https://www.syncfusion.com/feedback/23205/nullreferenceexception-occurs-when-the-underlying-collection-has-zero-items-and 
 
Note: The provider feedback link is private, and you need to login to view this feedback.

We will let you know once it is released. We appreciate your patience until then.

 
Regards,  
Vijayarasan S  



VS Vijayarasan Sivanandham Syncfusion Team April 6, 2021 12:43 PM UTC

Hi Geoffrey, 
Thank you for your patience. 
We are glad to announce that our latest weekly NuGet package update version 19.1.0.55 has been is rolled out with the “NullReferenceException occurs when the underlying collection has zero items and CheckBoxMappingName property is set in SfTreeGrid” and is available for download. 

Please let us know, if you require further assistance on this.

Regards,
Vijayarasan S
 


Loader.
Up arrow icon