The simplest example of using the OnPropertyChanged method in a project:
Private event PropertyChangedEventHandler propertychanged;
protected void OnPropertyChanged ([callermembername] String name = "")
{
PropertyChanged?. Invoke (this,new PropertyChangedEventArgs (name));
}
private int myVar;
public int MyProperty
{
get {return myVar;}
set {MyVar = value; OnPropertyChanged ("MyProperty"); }
}
For an instance of the TreeView two layer structure, the following code:
<treeview itemssource= "{Binding tvdatasource}" selecteditemchanged= "treeview_selecteditemchanged" > < treeview.itemtemplate>
Tvdatasource is a collection of tree structures with the object Treeviewdatamodel, which has the Name property, nodes collection. The Treeviewnodedatamodel object is stored in the Nodes collection.
So in the background only need to assign values to the Tvdatasource collection, the tree structure will show two-level structure. And can be deleted by clicking on the tree structure.
Object:
public class Treeviewdatamodel {public Treeviewdatamodel () { Nodes = new observablecollection< Treeviewnodedatamodel> (); } public string Name {get; set;} Private observablecollection<treeviewnodedatamodel> nodes; Public observablecollection<treeviewnodedatamodel> Nodes { get {return Nodes;} set {nodes = value; OnPropertyChanged ("Nodes"); } } private Event PropertyChangedEventHandler propertychanged; protected void OnPropertyChanged ([callermembername] String name = "") { propertychanged?. Invoke (This, new PropertyChangedEventArgs (name)); } public class Treeviewnodedatamodel {public string Name {get; set;} } }
Assignment value:
Private observablecollection<treeviewdatamodel> Tvdatasource; Public observablecollection<treeviewdatamodel> tvdatasource { get {return tvdatasource;} set { Tvdatasource= value; OnPropertyChanged ("Tvdatasource"); } }
Tvdatasource = new observablecollection<treeviewdatamodel> ();
Treeviewdatamodel TV = new Treeviewdatamodel ();
Tv. Name = "A";
Treeviewnodedatamodel Tvnode = new Treeviewnodedatamodel ();
Tvnode.name = "a";
Tv. Nodes.Add (Tvnode);
TVDATASOURCE.ADD (TV);
WPF-MVVM's TreeView