We introduced how to bind data to ListBox last time. Today we bind data to Treeview and menu. The difference is that datatemplate. Instead of datatemplate, hierarchicaldatatemplate is used.
This is a layered data template, and hierarchical is a layered data template. Datatemplate is a data template. This is easy to remember.
We still use the last data.
Let's look at. XAML.
Code <Window. Resources> <objectdataprovider X: Key =" ODP "Objecttype =" {X: Type SRC: leaguelist} "/> <Hierarchicaldatatemplate datatype =" {X: Type SRC: League} "Itemssource =" {Binding Path = divisions} "> <Textblock text =" {Binding Path = Name} "/> </Hierarchicaldatatemplate> {X: Type SRC: Division} "Itemssource =" {Binding Path = teams} "> <Textblock text =" {Binding Path = Name} "/> </Hierarchicaldatatemplate> <datatemplate datatype =" {X: Type SRC: Team} "> <Textblock text =" {Binding Path = Name} "/> </Datatemplate> </window. Resources>
Do not forget to add your own namespace SRC.
Datatype in hierarchicaldatatemplate is: gets or sets the type for this datatemplate.
Itemssource is a data class bound to the next layer.
Treeview binding
Next, let's look at the Treeview:
Code<Treeview Height ="200"Horizontalalignment ="Left"Margin =", 0"Name ="Treeview1"Verticalalignment ="Top"Width ="167"> <Treeviewitem header =" competition team "itemssource ="{Binding source = {staticresource ODP }}"> </Treeviewitem> </Treeview>
You can press f5.
Menu binding
Code<Menu Height ="23"Horizontalalignment ="Left"Margin =", 0"Name ="Menu1"Verticalalignment ="Top"Width ="167"> <Menuitem header =" competition team "itemssource ="{Binding source = {staticresource ODP }}"> </Menuitem> </menu>
Menu binding is much easier.