Use of the TreeView in WPF

Source: Internet
Author: User

Because the TreeView control is needed in the project, because it is used in WPF for the first time, a lot of methods about data binding have been searched on the internet beforehand, and individuals have actually applied it, and feel that WPF The Hierarchicaldatatemplate definition template is a lot more useful, but today there is another way to do it in your own WPF+MVVM project. The code is inappropriate, look at enlighten.

First, data binding:

1, front desk xmal code: (no template definition used)

<treeview name= "TreeView"/>

/2, XAML Interactive logic CS code in the background adds a data context and passes the TreeView as a parameter to the corresponding ViewModel

Public TreeView ()
{
InitializeComponent ();
This. DataContext = new TREEVIEWVM (This.treeview);
}

3. Define entity classes, examples here    

Contry: County entity Class (attribute description automatically extracts the description information of database fields, corresponding to the Contry table in the database)

public class Contry
{
public Contry ();

String Mc{get;set;}

int Id{get;set;}

int Parentid{get;set}

}

/4, implementing data Binding in TREEVIEWVM     

constructor to receive the TreeView object passed by the foreground.

Public Treeviewvm (TreeView tr)
{
This.treeview = TR;
Initialcommand ();
Refreshtreeview (); Refresh TreeView to get Data
}

<summary>
Create the item for the TreeView
</summary>
<param name= "" ></param>
<returns></returns>
Private TreeViewItem Createtreeviewitem (Contry cty)
{
TreeViewItem TVI = new TreeViewItem ();
TVi. Header = Cty. MC;
TVi. Tag = Cty;
TVi. isexpanded = true;//Set data list to expand
return TVI;
}

<summary>
Gets the first-level node in the TreeView and implements recursive traversal
</summary>
<param name= "Contrylist" ></param>
<returns></returns>
Public List<contry > Bind (List<contry > Contrylist)
{

Contrylist= diccachelist.instance.getlistbytablename<contry > (TRUE);//Gets a list collection of Contry data tables in the cached dictionary, The specific encapsulation method is omitted here;
var list = Contrylist.where (a = a.parentid== null | | a.parentid== "_"). ToList ();//The ParentID (parent ID) of the first-level node in the database is empty or "_", where the data collection of the first-level node is obtained;

if (list. Count = = 0)
{
return null;
}
List. ForEach (A =
{
TreeViewItem TVI1 = Createtreeviewitem (a);
Treeview. Items.Add (TVI1);
Finddownward (Contrylist, a.id, TVI1);
});
return null;
}

<summary>
Implementation method of Recursive traversal treeview
</summary>
<param name= "" ></param>
<param name= "id" ></param>
<param name= "TVI" ></param>
<returns></returns>
Public Contry Finddownward (list<contry> contrylist, string ID, TreeViewItem TVI)
{
if (contrylist = = null)
{
return null;
}
var list = Contrylist.where (a = a.parentid== ID). ToList ();

if (list. Count = = 0)
{
return null;
}
List. ForEach (A =
{
TreeViewItem Tvi2 = Createtreeviewitem (a);
TVi. Items.Add (TVI2);
Finddownward (Contrylist, a.id, TVI2);
});
return null;
}

<summary>
Get the TreeView list, refresh the list
</summary>
private void Refreshtreeview ()
{
var contrylist= diccachelist.instance.getlistbytablename<contry> (True);
Treeview. Items.clear ();
Bind (contrylist);
}

Use of the TreeView in WPF

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.