× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

Delete item in nested collections of Nth level

Asad Saeeduddin
Oct 08, 2015
<p>I would deal with this by making a small change to my design (assuming the snippet in your question is pseudocode for a class):</p> <pre><code>TreeNode { string name; TreeNode Parent; ObservableCollection&lt;TreeNode&gt; Children; public void Delete() { Parent.Children.Remove(this); } } </code></pre> <p>This makes a little bit more work for you maintaining an extra reference when manipulating your object graph, but saves you a lot of effort and code when doing things like deletes as you can see above.</p> <p>You haven't shown how you're constructing <code>TreeNode</code>s, but I'd make the parent and a collection for the children arguments of the constructor.</p> <p>This tip was originally posted on <a href="http://stackoverflow.com/questions/30689905/Delete%20item%20in%20nested%20collections%20of%20Nth%20level/30690054">Stack Overflow</a>.</p>
comments powered by Disqus