Hello,
I think I found a bug with the ParentItem property of a node. When a node gets added to another, the child node's ParentItem node is always null. I think based on the documentation, this should be the reference to the parent node. Is there any work around to get the parent node of a child? Thanks.
Infragistics.Controls.Menus.XamTreeItem ndeRoot = new Infragistics.Controls.Menus.XamTreeItem();ndeRoot.Header = "RootItem";
Infragistics.Controls.Menus.XamTreeItem ndeChild = new Infragistics.Controls.Menus.XamTreeItem();ndeChild.Header = "ChildItem";ndeRoot.Items.Add(ndeChild);
this.XamTree1.Items.Add(ndeRoot);
if (ndeChild.ParentItem == null) { System.Windows.MessageBox.Show("Parent Null");} else { System.Windows.MessageBox.Show("Parent Not Null");}
Hi,
This isn't actually a bug. ParentItem refers to the Data object that owns the child. So if you were bound, it would refer to the data objects that owns the child. In the case where you're adding controls directly to the control, then it refers to the control. However, parent associations only occur when the control is actually added to the visualTree, So after the parent is expanded, since the child added to the Items collection could be anything, the only way to know if the XamTreeItem actually exists is once its actually invoked naturally through the ItemsControl framework.
-SteveZ
Thanks Steve, I understand, sorry. Is there any way to get the parent node then if you are adding the node to the tree programatically like my example?
Thanks.
So what exactly are you trying to do?
In the example you gave, you know who the parent is, b/c you added directly to it. So, i'm guessing your actual scenario is a bit more advanced.
The more information you can give, the better. Perhaps there is actually a better way to achieve what you're looking for than just looking for the parent.
Thanks Steve, yeah the example is a little more simplified than what I'm working with, but basically I have to load a tree, then default some items to be checked. Then after they are loaded, go through and find the items that are checked, and only expand the parent items that are checked so that you can see all the items that checked. My tree in the real code is several layers deep. Here is sort of what I'm trying to do, hope it makes sense.
As always thanks so much for your help.
private void MainPage_Loaded(object sender, System.Windows.RoutedEventArgs e){ Infragistics.Controls.Menus.XamTreeItem ndeRoot = new Infragistics.Controls.Menus.XamTreeItem(); ndeRoot.Header = "RootItem";
Infragistics.Controls.Menus.XamTreeItem ndeChild = new Infragistics.Controls.Menus.XamTreeItem(); ndeChild.Header = "ChildItem"; ndeChild.Name = "Child1"; ndeChild.CheckBoxVisibility = Infragistics.ExtendedVisibility.Visible; ndeChild.IsChecked = true; ndeRoot.Items.Add(ndeChild);
//The child will be added, but the parent will still be collapsed this.XamTree1.Items.Add(ndeRoot);
this.ExpandChildNodesThatAreChecked();}
private void ExpandChildNodesThatAreChecked(){ Infragistics.Controls.Menus.XamTreeItem nde = this.XamTree1.FindName("Child1");
//Next, I need to expand the parnet (RootItem) in this example Infragistics.Controls.Menus.XamTreeItem ndeParent = null;
//Get the parent from the child //ndeParent = nde.ParentItem (I know this is wrong now) //ndeParent.IsExpanded = True}
Thanks Steve, yeah I'll just walk through the nodes and check for children that are checked. I would use MVVM but we're actually trying to convert code from a Win Forms project and trying to keep as parrallel as possible. (That's why I was searching for the parent node because it's actually the Infragistics WinForms control I'm manipulating. )
Thanks for all your help Steve.
Can you start from the root of the tree and work your way down?
foreach(XamTreeItem item in this.xamTree.Items) { item.IsExpanded = item.IsChecked;}
Also, have you thought about following the MVVM pattern and have a ViewModel that you walk through instead of the XamTreeItems. Then you'd just update properties on your ViewModel, and have bindings to the IsChecked and IsExpanded properties.