WPF-MVVM's TreeView

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.