c# - WPF TreeView-How to refresh tree after adding/removing node? -


i refer article:

wpf treeview hierarchicaldatatemplate - binding object multiple child collections

and modify tree structure like:

root   |__group        |_entry            |_source 

in entry.cs:

public class entry {     public int key { get; set; }     public string name { get; set; }      public observablecollection<source> sources { get; set; }      public entry()     {         sources = new observablecollection<source>();     }      public observablecollection<object> items     {                 {             observablecollection<object> childnodes = new observablecollection<object>();              foreach (var source in this.sources)                 childnodes.add(source);              return childnodes;         }     } } 

in source.cs:

public class source {     public int key { get; set; }     public string name { get; set; } } 

in xaml file:

<usercontrol.commandbindings>     <commandbinding command="new" executed="add" /> </usercontrol.commandbindings>      <treeview x:name="treeview">         <treeview.itemcontainerstyle>             <style targettype="{x:type treeviewitem}">                 <setter property="treeviewitem.isexpanded" value="true"/>             </style>         </treeview.itemcontainerstyle>          <treeview.resources>             <hierarchicaldatatemplate datatype="{x:type local:root}" itemssource="{binding items}">                  <textblock text="{binding path=name}" isenabled="true">                  </textblock>             </hierarchicaldatatemplate>              <hierarchicaldatatemplate datatype="{x:type local:group}" itemssource="{binding items}">                 <textblock text="{binding path=name}" isenabled="true">                 </textblock>             </hierarchicaldatatemplate>               <hierarchicaldatatemplate datatype="{x:type local:entry}" itemssource="{binding items}">                 <stackpanel orientation="horizontal">                     <textblock text="{binding path=name}" isenabled="true">                         <textblock.contextmenu>                             <contextmenu >                                 <menuitem header="add" command="new">                                 </menuitem>                             </contextmenu>                         </textblock.contextmenu>                     </textblock>                 </stackpanel>             </hierarchicaldatatemplate>               <datatemplate datatype="{x:type local:source}" >                 <textblock text="{binding path=name}" />             </datatemplate>          </treeview.resources>     </treeview> 

in usercontrol.cs:

public observablecollection<root> roots = new observablecollection<root>();      public usercontrol6()     {         initializecomponent();          //...add new node manually          treeview.itemssource = roots;     }      private void add(object sender, executedroutedeventargs e)     {         entry ee = (entry)treeview.selecteditem;         source s3 = new source() { key = 3, name = "new source" };         ee.sources.add(s3);     } 

when click right button on specific node "entry" add new node "source" under entry (call "add" method), add new "source" object under entry successfully, can't see new node on treeview. how refresh treeview when adding/deleting node?

use observablecollection instead of ilist if want notify user interface in collection has changed


Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -