Question

In my program I would like to get all the children of a parent node so that I can add a new node to it. In the past I used this approach:

//This gets all the children of a parent node depending on it's DisplayName
var node = ObservableCollection.GetAllChildren(x => x.Children).Distinct().ToList().First(x => x.DisplayName == nameOfNode);
node.Children.Add(CreateNode(newNodeName));

Now I would like to GetAllChildren based off of my SelectedItem property, which is fully functional. Is it possible to do that with the GetAllChildren method? If so, how?

Addition 1:

My SelectedItem property is of type ViewModel. The ObservableCollection is also of type ViewModel, so basically SelectedItem tells the program which ViewModel is selected.

Addition 2:

I cannot just simply add to the Parent Node. I have to check how many children there are for other reasons in this program before adding.

Was it helpful?

Solution

I might be wrong but to me you seem to be asking how to find a node which equals SelectedItem and add a new node to it. Why dont you just use SelectedItem.Add instead??? It would sort of save you the searching by reference equals.

For counting the children just use the Linq Count() method :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top