We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

GridTreeControl - Notifying changes in collection and dealing with data object of selected node.

Hello. I'm now trying to use your GridTreeControl in my WPF Application. I used the sample from a dashboard, where collection of items was populated in ViewModel constructor. I made some changes into this sample and now I'm using EF context to get a list of objects for a collection from DB. Everything works pretty fine, and I see that GridTreeControl is populated after I start my app.
But now I'm wondering, how can I deal with:
1. Notifying GridTreeControl, when I add Items to the collection, because any changes to my collection aren'r rendereg in GridViewControl;
2. Getting the object, corresponding to selected node into my ViewModel, to pass some of it's properties into other controls and elements, using binding and INPC properties of my ViewModel.
Thank you.

3 Replies

JS Jayapradha S Syncfusion Team March 18, 2016 12:36 PM UTC

Hi Dzmitry,

Thank you for contacting Syncfusion support.

Regarding query: Notifying GridTreeControl, when I add Items to the collection, because any changes to my collection aren'r rendereg in GridViewControl;

When you dynamically loading nodes in GridTreeControl then the underlying collection will be updated when you add/update the itemssource.

Please find the sample below,
Sample Link: http://www.syncfusion.com/downloads/support/forum/123440/ze/TreeGrid_DynamicallyAddingNodes1614716107

Regarding Query: Getting the object, corresponding to selected node into my ViewModel, to pass some of it's properties into other controls and elements, using binding and INPC properties of my ViewModel.

You can get the selected node in selection changed event and getting this in viewmodel by using below code example:

this.treeGrid.Loaded += treeGrid_Loaded;

void treeGrid_Loaded(object sender, RoutedEventArgs e)

        {

            this.treeGrid.InternalGrid.SelectionChanged += InternalGrid_SelectionChanged;

        }


        void InternalGrid_SelectionChanged(object sender, GridSelectionChangedEventArgs e)

        {

            //getting the selected node

            var viewModel = this.treeGrid.DataContext as ViewModel;

            viewModel.GridTreeNode = (this.treeGrid.SelectedNode as GridTreeNode).Item;

            var parentItem = (this.treeGrid.SelectedNode as GridTreeNode).ParentItem;
        }



Regards,
Jayapradha



RI Richard March 19, 2016 03:45 PM UTC

Not sure if I am doing something wrong, but I tries the example mentioned above http://www.syncfusion.com/downloads/support/forum/123440/ze/TreeGrid_DynamicallyAddingNodes1614716107 

Although it did add the new child item when you click the button, there seems to be a problem. The new node is added and an expander icon is added to the new parent node. The state of this icon is wrong though. It initially appears as "expanded" though you cannot see the new node. You need to click it twice of actually close and open the new branch and then you can see you new node.

What can be done to fix that?

Also, how would you change it so that the new node added becomes the selected node?

thanks


JS Jayapradha S Syncfusion Team March 21, 2016 02:09 PM UTC

Hi Dzmitry,

In Our previous sample, we have added an item in parent node itself only and this item will not be added to the underlying collection. Hence this node is not reflected in view. We have modified the sample and you can see a newly added node without collapse/expand the tree grid.


Code Example:

var viewModel = this.treeGrid.DataContext as ViewModel;

         

            if (treeGrid.SelectedNodes.Count == 0)

            {

                viewModel.PersonDetails.Add(new PersonInfo() { LastName = "Sunil", FirstName = "Pradap", MyEyeColor = "Gray", DOB = new DateTime() });

                treeGrid.InternalGrid.SelectedNodes.Clear();

                treeGrid.InternalGrid.SelectedNodes.Add(this.treeGrid.InternalGrid.Nodes[treeGrid.InternalGrid.Nodes.Count - 1]);

                this.treeGrid.InternalGrid.Nodes[treeGrid.InternalGrid.Nodes.Count - 1].IsSelected = true;

            }

            else

            {

                var parentNodeItem = (treeGrid.SelectedNode as GridTreeNode).Item as PersonInfo;

                var parentNode = treeGrid.SelectedNodes[0];

                int parentIndex = this.treeGrid.InternalGrid.Nodes.IndexOf(parentNode);


                if (parentNodeItem.Children==null)

                {

                    parentNodeItem.Children = new ObservableCollection<PersonInfo>();

                }               

                parentNodeItem.Children.Add((new PersonInfo()

                {

                    LastName = "Sunil",

                    FirstName = "Pradap",

                    MyEyeColor = "Gray",

                    DOB = new DateTime()

                }));

               

                treeGrid.InternalGrid.SelectedNodes.Clear();

                this.treeGrid.InternalGrid.CurrentCell.Deactivate();

                this.treeGrid.InternalGrid.Model.Selections.Clear();

                treeGrid.InternalGrid.SelectedNodes.Add(this.treeGrid.InternalGrid.Nodes[parentIndex + parentNode.ChildNodes.Count]);

                this.treeGrid.InternalGrid.Nodes[parentIndex + parentNode.ChildNodes.Count].IsSelected = true;


            }



Regarding query: Also, how would you change it so that the new node added becomes the selected node?

You can set the new node as selected node by adding new node to selected nodes as shown in the following code example,

Code Example:

  treeGrid.InternalGrid.SelectedNodes.Clear();

 treeGrid.InternalGrid.SelectedNodes.Add(this.treeGrid.InternalGrid.Nodes[parentIndex + parentNode.ChildNodes.Count]);
 this.treeGrid.InternalGrid.Nodes[parentIndex + parentNode.ChildNodes.Count].IsSelected = true;


Please find the modified sample from the below location,

Sample Link: http://www.syncfusion.com/downloads/support/forum/123440/ze/TreeGrid_SelectAndAddNode-951831445

Regards,
Jayapradha

Loader.
Live Chat Icon For mobile
Up arrow icon