There are various ways to select items of Data tree like mouse click selection, check box based etc.
I have a special need for transfer nodes as below -
1) There will be 2 data trees as Source and Target and some buttons like TransferToSource, TransferToTarget to transfer nodes from one tree to another
2) Initially only Source Tree is populated with data and Target tree is blank.
3) TransferToTarget should work in a manner that all selected nodes from Source Tree should be transferred to Target tree while retaining their relative positions (hierarchy) in tree.
Example -
+ parent1
+ child1
- grandchild1
- grandchild2
+ child2
If grandchild1 above is selected for transfer, entire parent1, child1 and grandchild1 nodes should be transferred to other tree.
if child1 is selected for transfer, parent1, child1 should be transferred alongwith grandchild1 and grandchild2
Also when any nodes are transferred to target tree, they should be removed from source tree.
This should work for multiple selections too.
Let me know if you have any solutions to this.
Hello Shankar,
Thank you for your post. I have been looking into it and I am not completely sure what will happen with child2 and grandchild2 in the first case, because there won't be any parent node. Also if grandchild1 is selected for transfer, where should it go, as root element or somewhere else. All these manipulations should be done over the data source. You can get the data item from a node like this:
xamDataTree1.SelectionSettings.SelectedNodes
This will return a collection with XamDataTreeNode object. You can cast their Data Property to your Data object. If you use ObservableCollections the changes you made will automatically be reflected in the UI.
Thanks for your reply.
For the case you mentioned we need to follow the same parent hierarchy for Target Tree as Source Tree
So when grandchild1 is transferred, then parent1 and child1 should also be transferred alongwith grandchild1, but not child2 and grandchild2 since they are siblings for child1 and granchild1 respectively
As I said before if you are able to manipulate your data like this, you will be able to transfer nodes between the trees. I have already shared with you how to get the data from the selected nodes. You can use ObservbleCollections, so the adding and removing of the items could be reflected in the UI out of the box.
Thanks for reply.
Next big question is, the whole of this Transfer Nodes functionality needs to be packaged into some generic WPF control (may be user control) , say "XamDataTreeNodeTransferControl" so that it is reusable across various WPF windows.
This seems to be very tedious task since we would need expose bunch of properties of XamDataTree like ItemSource, GlobalNodeLayouts etc. as Dependency properties for User Control.
Do you have any better solution for this?
It seems like that making a UserControl and extend the XamDataTree is the best approach for achieving the functionality you want.
Thanks.
Can you create some sample for such user control and share with me?
I was wondering how do I expose and bind properties like "GlobalNodeLayouts" outside the user control. The trouble I see here is if we expose GlobalNodeLayouts as UserControl property, how do I pass on the values to set on USer Control to GlobalNodeLayouts of XamDataTree control living inside user control.
If you want to extend our XamDataTree control or use it as part of an UserControl I can suggest you contact with our consulting team here:
http://d3.infragistics.com/services/
They will be happy to assist you further and made all the custom functionalities you want.
Ok. I sent them email.
Also here is a sample application showing generic user control for transferring nodes from source to target tree. I seem to got it working as far as defining GloablNodeLayouts, ItemsSource as properties on user control. Only part left is add/remove nodes during transfer button click
If someone from you or consulting team can show how to implement removing nodes from source and adding to target, would be grateful.
Apparently doing Add/remove through XamDataTree.Nodes collection does not seem to work and I also can't do it directly into data given the fact that we are in the context of user control.
In my sample, I had written transfer function as below which was not complete though.
The logic was first assign Fresh ItemSource to xamDataTree2 which has complete list of nodes then remove nodes from xamDataTree2 as per user choice.
The problem was xamDataTree2.Nodes.Remove(node) did not seem to remove the node while running code in debug without any breakpoint
But when I added following line, highlighted in yellow, suddenly Node Remove started working which was surprising. I think doing xamDataTree2.Nodes.ToList() is updating the cache or doing something incredible.
private void moveRight_Click(object sender, RoutedEventArgs e)
{
List<object> total = new List<object>();
total.AddRange(TotalDataItems.OfType<object>());
xamDataTree2.ItemsSource = total;
var nodesToRemove = xamDataTree2.Nodes.Where(n1 => !xamDataTree1.SelectionSettings.SelectedNodes.Select(n2 => n2.Data).Contains(n1.Data)).ToList();
foreach (var node in nodesToRemove)
xamDataTree2.Nodes.Remove(node);
var temp = xamDataTree2.Nodes.ToList();
}
I made some further progress, but still could not get "MoveToRight" work.
Kindly go through attached is my sample app and let me know what I am missing.
I got the problem.
During Transfer function, I was removing nodes from Source Tree and adding to target tree but each of these trees have common items in their own ItemsSource. So removing nodes would cause removal of items from data which is shared by both tree which by design is correct but for my requirement is not correct.
I will figure out the solution soon.