GridTreeControl - Notifying changes in collection and dealing with data object of selected node.
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
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]); |
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
- 3 Replies
- 3 Participants
-
DZ Dzmitry
- Mar 17, 2016 01:10 PM UTC
- Mar 21, 2016 02:09 PM UTC