Untitled


SUBMITTED BY: Guest

DATE: Nov. 23, 2013, 6:11 p.m.

FORMAT: Text only

SIZE: 5.8 kB

HITS: 3774

  1. <Window x:Class="Test.MainWindow"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. Title="MainWindow" Height="350" Width="525">
  5. <Grid>
  6. <Grid.ColumnDefinitions>
  7. <ColumnDefinition Width="*"/>
  8. <ColumnDefinition Width="Auto"/>
  9. <ColumnDefinition Width="*"/>
  10. </Grid.ColumnDefinitions>
  11. <DataGrid Grid.Column="0" ItemsSource="{Binding Foos}" AutoGenerateColumns="False">
  12. <DataGrid.Columns>
  13. <DataGridTextColumn Header="Foo Name" Binding="{Binding Name}" Width="Auto" IsReadOnly="False" />
  14. </DataGrid.Columns>
  15. </DataGrid>
  16. <GridSplitter HorizontalAlignment="Right" VerticalAlignment="Stretch" Grid.Column="1" ResizeBehavior="PreviousAndNext" Width="5" Background="Gray" />
  17. <DataGrid Grid.Column="2">
  18. <DataGrid.Columns>
  19. <DataGridTextColumn Header="Bar Name" Width="Auto" IsReadOnly="False"/>
  20. </DataGrid.Columns>
  21. </DataGrid>
  22. </Grid>
  23. </Window>
  24. using System;
  25. using System.Windows;
  26. using System.Collections.ObjectModel;
  27. namespace Test
  28. {
  29. public class Foo
  30. {
  31. static int _nextId;
  32. public int Id { get; private set; }
  33. public String Name { get; set; }
  34. public ObservableCollection<Bar> Bars { get; set; }
  35. public Foo()
  36. {
  37. Id = _nextId++;
  38. Name = String.Empty;
  39. Bars = new ObservableCollection<Bar>();
  40. }
  41. }
  42. public class Bar
  43. {
  44. static int _nextId;
  45. public int Id { get; private set; }
  46. public String Name { get; set; }
  47. public Bar()
  48. {
  49. Id = _nextId++;
  50. Name = String.Empty;
  51. }
  52. }
  53. /// <summary>
  54. /// Interaction logic for MainWindow.xaml
  55. /// </summary>
  56. public partial class MainWindow : Window
  57. {
  58. public ObservableCollection<Foo> Foos { get; set; }
  59. public MainWindow()
  60. {
  61. Foos = new ObservableCollection<Foo>();
  62. Foo newFoo;
  63. for (int index = 0; index < 5; ++index)
  64. {
  65. newFoo = new Foo();
  66. newFoo.Name = String.Format("Foo {0}", index);
  67. Foos.Add(newFoo);
  68. }
  69. InitializeComponent();
  70. DataContext = this;
  71. }
  72. }
  73. }
  74. public partial class MainWindow : Window
  75. {
  76. public MainWindow()
  77. {
  78. InitializeComponent();
  79. DataContext = new ViewModel();
  80. }
  81. }
  82. public class ViewModel
  83. {
  84. public ViewModel()
  85. {
  86. Foos = new ObservableCollection<Foo>();
  87. }
  88. public ObservableCollection<Foo> Foos { get; set; }
  89. }
  90. public class Foo : INotifyPropertyChanged
  91. {
  92. static int _nextId;
  93. public int Id { get; private set; }
  94. public ObservableCollection<Bar> Bars { get; set; }
  95. public Foo()
  96. {
  97. Id = _nextId++;
  98. Name = String.Empty;
  99. Bars = new ObservableCollection<Bar>();
  100. }
  101. private string name;
  102. public string Name
  103. {
  104. get
  105. {
  106. return name;
  107. }
  108. set
  109. {
  110. name = value;
  111. Notify("Name");
  112. }
  113. }
  114. private void Notify(string propName)
  115. {
  116. if (PropertyChanged != null)
  117. PropertyChanged(this, new PropertyChangedEventArgs(propName));
  118. }
  119. public event PropertyChangedEventHandler PropertyChanged;
  120. }
  121. public class Bar : INotifyPropertyChanged
  122. {
  123. static int _nextId;
  124. public int Id { get; private set; }
  125. public Bar()
  126. {
  127. Id = _nextId++;
  128. Name = String.Empty;
  129. }
  130. private string name;
  131. public string Name
  132. {
  133. get
  134. {
  135. return name;
  136. }
  137. set
  138. {
  139. name = value;
  140. Notify("Name");
  141. }
  142. }
  143. private void Notify(string propName)
  144. {
  145. if (PropertyChanged != null)
  146. PropertyChanged(this, new PropertyChangedEventArgs(propName));
  147. }
  148. public event PropertyChangedEventHandler PropertyChanged;
  149. }
  150. <DataGrid Grid.Column="2" ItemsSource="{Binding ElementName=gridTop, Path=SelectedItem.Bars}">
  151. <DataGrid.Columns>
  152. <DataGridTextColumn Header="Bar Name" Binding="{Binding Name}" Width="Auto" IsReadOnly="False"/>
  153. </DataGrid.Columns>
  154. </DataGrid>
  155. DataContext="{Binding ElementName=gridTop, Path=SelectedItem.Bars}"

comments powered by Disqus