c# - WPF TreeView ObservableCollection notifying sample code -
i'm trying write c# wpf application , i'm stuck treeview , observablecollection.
this treeview items.
| root --- subitem ------ subitem | root --- subitem ------ subitem ---------- subitem i'm modifyng items other window , need update treeview without re-loading items. i've made search , found observablecollection. can't understand how utilize observablecollection , notify changes , update list. can give me sample code or help me doing that?
here illustration implement simplifying wpf treeview using viewmodel pattern.
this sample,
your model:
public interface ifolder { string fullpath { get; } string folderlabel { get; } observablecollection<ifolder> folders { get; } } your viewmodel:
class viewmodel : inotifypropertychanged { public viewmodel() { m_folders = new observablecollection<ifolder>(); //add root items folders.add(new folder { folderlabel = "dummy1", fullpath = @"c:\dummy1" }); folders.add(new folder { folderlabel = "dummy2", fullpath = @"c:\dummy2" }); folders.add(new folder { folderlabel = "dummy3", fullpath = @"c:\dummy3" }); folders.add(new folder { folderlabel = "dummy4", fullpath = @"c:\dummy4" }); //add sub items folders[0].folders.add(new folder { folderlabel = "dummy11", fullpath = @"c:\dummy11" }); folders[0].folders.add(new folder { folderlabel = "dummy12", fullpath = @"c:\dummy12" }); folders[0].folders.add(new folder { folderlabel = "dummy13", fullpath = @"c:\dummy13" }); folders[0].folders.add(new folder { folderlabel = "dummy14", fullpath = @"c:\dummy14" }); } public string test { get; set; } private observablecollection<ifolder> m_folders; public observablecollection<ifolder> folders { { homecoming m_folders; } set { m_folders = value; notifiypropertychanged("folders"); } } void notifiypropertychanged(string property) { if (propertychanged != null) propertychanged(this, new propertychangedeventargs(property)); } public event propertychangedeventhandler propertychanged; } in xaml:
<textblock text="simple root binding" foreground="red" margin="10,10,0,0" /> <treeview itemssource="{binding folders}" margin="10"> <treeview.itemtemplate> <datatemplate> <treeviewitem header="{binding folderlabel}"/> </datatemplate> </treeview.itemtemplate> </treeview> full code
c# wpf treeview observablecollection
No comments:
Post a Comment