To create a tree node:
(1) : declare a tree node class to store node icons, names, types, ID S, and subsets.
(2) : build a TreeView resource in XAML to display node data.
(3) : define tree node data in logic code and assign it to class;
(4) : assign value to ItemSource of TreeView.
Example:
(1)
//In the folder "base", declare TreeView class to store icon, name and leaf node collection
public class TV()
{
public string TvIcon { get ; set ; }
public string TvName { get ; set ; }
public List TvList { get ; set ; }
}
(2)
Note: [1] add a reference to the class "TV()" in the namespace. I name it "myTree". For example,
< UserControl X: class = "client. Basis. Usercontrol3"
...
xmlns:myTree = "CLR namespace: client. / 10Base"
<TreeView Name="myTreeView"> <TreeView.Resources> <HierarchicalDataTemplate DataType="{x:Type myTree:TV }" ItemsSource = "{ Binding TvList , Mode = TwoWay , UpdateSourceTrigger=PropertyChanged }"> <StackPanel Orientation="Horizontal" Margin="0,2" > <Image Height="18" Source="{ Binding TvIcon,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged }"> </Image> <TextBlock Text="{Binding TvName,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged }"> </TextBlock> </StackPanel> </HierarchicalDataTemplate> </TreeView.Resources> </TreeView >
Note [1]: for a new class, "cannot find..." often appears "Does not exist..." You need to "rebuild the solution" or restart "VS" after confirming that the code is correct and the path of the class is correct.
(3): [1]: Reference namespace first using Client._10Base; [2]:Declare a leaf node collection private static List<TV> TVList1; private static List<TV> TVList2; private static List<TV> SelectTreeView() { TVList = new List<TV>(); for( int i = 0; i < 5 ; i ++ ) { TVList2 = new List<TV>(); for( int r = 0 ; r < 2 ; r ++ ) { TV tvList2 = new TV(); tvList2.TvIcon = "/../../Image/Afoot2.png" tvList2.TvName = "In the second level of child nodes"+r+"Node"; tvList2.TvList = new List<TV>(); TVList2.Add(tvList2); } TV tvList1 = new TV(); tvList1.TvIcon = "/../../Images/Afoot3.png"; tvList1.TvName = "First level child node"+i+""; tvList1.TvList = TVList2; TVList1.Add(tvList1); } List<TV> TV0 = new List<_10Base.TV>() { new TV(){ TvIcon = "/../../Images/Afoot1.png", TvName = "Top level parent", TvList = TVList1, } }; return TV0; } (4) private void Main() { List<TV> stvTask = SelectTreeView(); this.myTreeView.ItemsSource = stvTask ; }
Note: [1]: declare the composition of namespace in (Interface) XAML: "project name + folder name";
And declaring the namespace in (logical) code: "project name + price folder name";
They are the same composition. If the class actually exists, it will display "does not exist..." in the namespace , click on the project and select rebuild solution.
[2] : for the fields of the tree node's class, you can define unlimited fields. For example,
public class TV
{
//You can also use constructors to initialize
public TV()
{
this.TvIcon = “/… ... /Default icon required ";
this.TvName = "default name";
}
public string TvIcon { get; set; }
public string TvName { get; set; }
public List TvList { get; set; }
public TvType TvType { get; set; }
public string TvID { get; set; }
}
public enum TvType
{
TvFather, / / root
TvSon, / / leaf
TvStruct / / structure
}
3. Call the members in the class in the interface, using the Binding keyword:
If the called field is not found in the class, the content is not displayed. It doesn't report a mistake.