c# - WPF-TreeView DataBinding in MVVM -
i seek implement simple mvvm-model wpf-treeview databinding. found problems , hope, of can help me it.
i have model, defind own class:
public class itemofworld : inotifypropertychanged { #region fields private observablecollection<itemofworld> _items; private string _name; private itemofworld _parent; #endregion #region fielddeclaration public observablecollection<itemofworld> items { { homecoming _items; } set { _items = value; onpropertychanged("items"); } } public string name { { homecoming _name; } set { _name = value; onpropertychanged("name"); } } public itemofworld parent { { homecoming _parent; } set { _parent = value; onpropertychanged("parent"); } } #endregion public itemofworld(itemofworld parent) { items = new observablecollection<itemofworld>(); parent = parent; } #region inotifypropertychanged members public event propertychangedeventhandler propertychanged; private void onpropertychanged(string propertyname) { propertychangedeventhandler handler = propertychanged; if (handler != null) { handler(this, new propertychangedeventargs(propertyname)); } } #endregion } public class myworld:itemofworld { public myworld():base(null) { name = "myworld"; items.add(new ions(this)); } } public class ions : itemofworld { public ions(itemofworld parent) : base(parent) { name = "ions"; parent = parent; } } public class molecules : itemofworld { public molecules(itemofworld parent) : base(parent) { name = "molecules"; parent = parent; } } i have viewmodel, create myworld class:
internal class itemsofworldviewmodel { #region fields private itemofworld _myitem; #endregion #region fielddeclaration public itemofworld myitem { { homecoming _myitem; } set { _myitem = value; } } #endregion public itemsofworldviewmodel() { myitem = new myworld(); } } and, finally, have view treeview toolbox, simple code-behind:
itemsofworldviewmodel myworld=new itemsofworldviewmodel(); treeview1.datacontext = myworld; my question is: how can show in treeview hierarchy without using local-sources?
-myworld -->ions -->molecules if implement simple textblock works without additional libraries , local-sources, simple datacontext in code-behind , in xaml:
<textbox x:name="textblock2" text="{binding myitem.name}"></textbox>
use hierarchicaldatatemplate treeview.itemtemplate
<treeview itemssource="{binding myitem.items}"> <treeview.resources> <hierarchicaldatatemplate datatype="{x:type local:itemofworld }" itemssource = "{binding path=items}"> <textblock text="{binding path=name}"/> </hierarchicaldatatemplate> </treeview.resources> </treeview> c# wpf xaml mvvm treeview
No comments:
Post a Comment