Advertisement
Jmat4119

Xaml and code behind for multilevel binding

Aug 9th, 2013
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. <Window x:Class="MultiMedialab.MainWindow"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:local="clr-namespace:MultiMedialab"
  5. DataContext="{Binding RelativeSource={RelativeSource Self}}"
  6. Title="MainWindow" Height="350" Width="525"
  7.  
  8. Loaded="Window_Loaded">
  9. <Window.Resources>
  10. <!-- Book Chapters -->
  11. <DataTemplate DataType="{x:Type local:BookChapter}">
  12. <StackPanel Orientation="Horizontal">
  13. <TextBlock Text="{Binding Path=Name}" />
  14. <ItemsControl ItemsSource="{Binding Path=Pages}" />
  15. </StackPanel>
  16. </DataTemplate>
  17.  
  18. <!-- Book Pages -->
  19. <DataTemplate DataType="{x:Type local:BookPage}">
  20. <StackPanel Orientation="Horizontal" >
  21. <TextBlock Text="{Binding Path=Id}" Margin="5 0"/>
  22. <TextBlock Text="{Binding Path=Name}" Margin="5 0"/>
  23. <ItemsControl ItemsSource="{Binding Path=Exercises}" />
  24. </StackPanel>
  25. </DataTemplate>
  26.  
  27. </Window.Resources>
  28. <Grid>
  29. <ListBox ItemsSource="{Binding Path=Chapters}" Name="lboxTest" />
  30. </Grid>
  31. </Window>
  32.  
  33. /// <summary>
  34. /// Interaction logic for MainWindow.xaml
  35. /// </summary>
  36. public partial class MainWindow : Window
  37. {
  38. public MainWindow()
  39. {
  40. InitializeComponent();
  41. }
  42. ObservableCollection<BookChapter> chapterItm = new ObservableCollection<BookChapter>();
  43. public ObservableCollection<BookChapter> Chapters
  44. { get { return chapterItm; } }
  45.  
  46. private void Window_Loaded(object sender, RoutedEventArgs e)
  47. {
  48. List<string> stringcol1 = new List<string>();
  49. stringcol1.Add("Excercise 1");
  50. stringcol1.Add("Excercise 2");
  51.  
  52. List<string> stringcol2 = new List<string>();
  53. stringcol2.Add("Excercise 5");
  54. stringcol2.Add("Excercise 6");
  55.  
  56. List<BookPage> subList1 = new List<BookPage>();
  57. subList1.Add(new BookPage { Name = "Page 1 ", Id = "ID1", Excercises = stringcol1});
  58. subList1.Add(new BookPage { Name = "Page 2", Id = "ID2", Excercises = stringcol2 });
  59.  
  60. List<BookPage> subList2 = new List<BookPage>();
  61. subList2.Add(new BookPage { Name = "Page 5", Id = "ID 21", Excercises = stringcol1 });
  62. subList2.Add(new BookPage { Name = "Page 6", Id = "ID22", Excercises = stringcol2 });
  63.  
  64. chapterItm.Add(new BookChapter { Name = "Chapter 1", Pages = subList1});
  65. chapterItm.Add(new BookChapter { Name = "Chapter 2", Pages = subList2 });
  66.  
  67. }
  68. }
  69. public class BookChapter
  70. {
  71. public string Name { get; set; }
  72. public List<BookPage> Pages { get; set; }
  73. }
  74. public class BookPage
  75. {
  76. public string Id { get; set; }
  77. public string Name { get; set; }
  78. public List<String> Excercises { get; set; }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement