WPF TreeView Extension

Source: Internet
Author: User

Keep a record of the problems encountered in your work so that you can forget about them later.

A problem encountered at work is to implement the following type of interface, without using the Telerik and dev libraries. Originally wanted to use the DataGrid, but did not know how to implement the TreeView, and then use the TreeView.

Front-end XAML code:

<treeview x:name= "MyTreeView" >
<TreeView.ItemTemplate>
<Grid>
<Grid.ColumnDefinitions>
<columndefinition width= "{Binding level,converter={staticresource Leveltowidthconverter}}"/>
<columndefinition width= "*"/>
</Grid.ColumnDefinitions>
<textblock text= "{Binding Content}"/>
<checkbox ischecked= "{Binding IsChecked}"
grid.column= "1"/>
</Grid>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>

The following controls the distance between the text and the checkbox.

<columndefinition width= "{Binding level,converter={staticresource Leveltowidthconverter}}"/>

Background code:

private void Initdatabase ()
{
list<mytreeview> lists = new list<mytreeview> ();
MyTreeView node = new MyTreeView ("Classification", false, 0);
MyTreeView Node1 = new MyTreeView ("fruit", false,1);
MyTreeView Node2 = new MyTreeView ("Color", false, 1);
MyTreeView node3 = new MyTreeView ("vegetable", false, 1);
MyTreeView node4 = new MyTreeView ("Apple", false, 2);
MyTreeView node5 = new MyTreeView ("Red", false, 2);
MyTreeView node6 = new MyTreeView ("green", false, 2);
MyTreeView Node7 = new MyTreeView ("tomato", false, 2);

Node1. Nodes.Add (NODE4);
Node2. Nodes.Add (NODE5);
Node2. Nodes.Add (NODE6);
Node3. Nodes.Add (NODE7);
Node. Nodes.Add (Node1);
Node. Nodes.Add (Node2);
Node. Nodes.Add (NODE3);
Lists. ADD (node);
MyTreeView. ItemsSource = lists;
}
}


public class Mytreeview:inotifypropertychanged
{
Public MyTreeView (String Content,bool ischecked,int level)
{
This. Content = content;
This. IsChecked = IsChecked;
This.level = level;
Nodes = new list<mytreeview> ();
}

public string Content {get; set;}


public bool IsChecked {get; set;}


public int level {get; set;}

Public list<mytreeview> Nodes {get; set;}


public event PropertyChangedEventHandler PropertyChanged;
}

Width conversion:

public class Leveltowidthconverter:ivalueconverter
{
public object Convert (object value, Type targetType, object parameter, CultureInfo culture)
{
if (value = = NULL | | (int) value = = 0)
return 200;
return-(int) value * 20;
}

public object Convertback (object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException ();
}
}

WPF TreeView Extension

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.