xaml - Bind subitems along with main items to same control WPF -


how can bind object list control main items , sub items. if class structure this

public class bookchapter {  public string name { get; set; }  public list<bookpage> pages {get; set; }  public list<string> questions { get; set; }  } public class bookpage {     public string id { get; set; }     public string name { get; set; }     public list<string> excercises { get; set; } } 

how can bind on wpf control chaptername , pages associated every chapters in single control.

there few ways achieve in wpf, 1 way involve use of datatemplates describe how objects displayed. using itemscontrol or derived controls hold display collections:

i separate styles various types make them easier read e.g:

<window x:class="wpfapplication2.mainwindow"     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     xmlns:local="clr-namespace:wpfapplication2"     title="mainwindow" height="350" width="525"     datacontext="{binding relativesource={relativesource self}}"> <window.resources>      <!-- book chapters -->     <datatemplate datatype="{x:type local:bookchapter}">         <stackpanel orientation="horizontal">             <textblock text="{binding path=name}" />             <itemscontrol itemssource="{binding path=pages}" />         </stackpanel>     </datatemplate>      <!-- book pages -->     <datatemplate datatype="{x:type local:bookpage}">         <stackpanel orientation="horizontal" >             <textblock text="{binding path=id}" margin="5 0"/>             <textblock text="{binding path=name}" margin="5 0"/>             <itemscontrol itemssource="{binding path=exercises}" />         </stackpanel>     </datatemplate>  </window.resources> <grid>     <listbox itemssource="{binding path=chapters}" /> </grid> 

which produce looks (based on populated objects with):enter image description here

without templating objects, wpf display collections applicationname.classname - result of tostring, templates give ability define how classes displayed.

you can hold these templates in separate resource file, , reuse them across application if required. it's important note because i'm templating actual type, template used whenever objects displayed within particular window, unless specify otherwise.

you can add greater control on when/were these templates used, naming them, , applying them specific components (http://msdn.microsoft.com/en-us/library/ms742521.aspx).


Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -