c# - Get the Parent List in a TreeView -
my problem following: have treeview, bounded itemssource. selecteditem bound property in viewmodel called selecteditem.
the itemssource build like:
public class item { public header { get; set; } public observablecollection<item> itemchildren { get; set; } . . . }
i want delete, reorder... items. need parent list of selecteditem this. research lists item, item can exists twice , separators item can null.
does knows how can parent list of selected item?
thank u much!
regards, kevin
if tree, don't need list of parents reference parent:
public class item { public item() { _col = new observablecollection<item>(); _col.collectionchanged += new notifycollectionchanged(itemchildren_collectionchanged); } public header { get; set; } public observablecollection<item> itemchildren { { return _col; } } public item parent { { return _parent; } } private void itemchildren_collectionchanged( object sender, notifycollectionchangedeventargs e) { // newly added items: item._parent = this; // removed items: item._parent = null; } private item _parent; private observablecollection<item> _col; }
this solution more complex started with. parent object must track children , make sure child object's parent set every time when child added it.
i have been typing code without syntax checking, hits point. can find whole source code same question in article: how implement collection property contain unique objects.
edit: note item
class in code above not allow callers set itemchildren
collection. collection created , served item instance , nobody allowed change it. allowed add/remove items in it. 1 more thing way in item handles newly added child items - free throw invalidoperationexception
if attempts add item non-null parent reference! ensure structure remains tree. example, xmlnode
class (system.xml
) throws invalidoperationexception
appendchild
method if argument ancestor of current node. possibilities endless when item
has opportunity verify elements added it.
Comments
Post a Comment