Guest User

Untitled

a guest
Sep 14th, 2015
166
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // C# part:
  2. class NotifyPropertyChangedImpl : INotifyPropertyChanged
  3. {
  4. protected void NotifyPropertyChanged([CallerMemberName] string name = "")
  5. {
  6. if (PropertyChanged != null)
  7. PropertyChanged(this, new PropertyChangedEventArgs(name));
  8. }
  9.  
  10. public event PropertyChangedEventHandler PropertyChanged;
  11. }
  12.  
  13. class Data : NotifyPropertyChangedImpl
  14. {
  15. private bool _isParentSelected;
  16.  
  17. public bool IsParentSelected
  18. {
  19. get { return this._isParentSelected; }
  20. set
  21. {
  22. this._isParentSelected = value;
  23. NotifyPropertyChanged();
  24. }
  25. }
  26.  
  27. public string Title { get; set; }
  28. }
  29.  
  30. class ParentData : NotifyPropertyChangedImpl
  31. {
  32. private bool _isSelected;
  33. public bool IsSelected
  34. {
  35. get { return this._isSelected; }
  36. set
  37. {
  38. this._isSelected = value;
  39. NotifyPropertyChanged();
  40. if (First != null)
  41. First.IsParentSelected = value;
  42. if (Second != null)
  43. Second.IsParentSelected = value;
  44. }
  45. }
  46.  
  47. public Data First { get; set; }
  48. public Data Second { get; set; }
  49. }
  50.  
  51. class MainVM
  52. {
  53. public IEnumerable<ParentData> Hierarchical { get; set; }
  54. public IEnumerable<Data> Flat { get; set; }
  55. }
  56.  
  57. public partial class App : Application
  58. {
  59. protected override void OnStartup(StartupEventArgs e)
  60. {
  61. base.OnStartup(e);
  62. var data = new[]
  63. {
  64. new ParentData()
  65. {
  66. First = new Data() { Title = "Big" },
  67. Second = new Data { Title = "Bad" }
  68. },
  69. new ParentData()
  70. {
  71. First = new Data() { Title = "Gray" },
  72. Second = new Data { Title = "Wolf" }
  73. }
  74. };
  75.  
  76. var mainVM = new MainVM()
  77. {
  78. Hierarchical = data,
  79. Flat = data.SelectMany(pd => new[] { pd.First, pd.Second }).ToArray()
  80. };
  81.  
  82. var mainWindow = new MainWindow() { DataContext = mainVM };
  83. mainWindow.Show();
  84. }
  85. }
  86.  
  87. <!-- XAML part -->
  88.  
  89. <Application x:Class="Test.App"
  90. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  91. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  92. </Application>
  93.  
  94. <Window x:Class="Test.MainWindow"
  95. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  96. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  97. xmlns:local="clr-namespace:Test"
  98. Title="MainWindow" Height="350" Width="525">
  99. <Grid>
  100. <Grid.ColumnDefinitions>
  101. <ColumnDefinition/>
  102. <ColumnDefinition/>
  103. </Grid.ColumnDefinitions>
  104.  
  105. <DataGrid ItemsSource="{Binding Hierarchical}" AutoGenerateColumns="False">
  106. <DataGrid.ItemContainerStyle>
  107. <Style TargetType="DataGridRow">
  108. <Setter Property="IsSelected" Value="{Binding IsSelected}"/>
  109. </Style>
  110. </DataGrid.ItemContainerStyle>
  111. <DataGrid.Columns>
  112. <DataGridTemplateColumn Header="First">
  113. <DataGridTemplateColumn.CellTemplate>
  114. <DataTemplate DataType="{x:Type local:ParentData}">
  115. <TextBlock Text="{Binding First.Title}" Name="tb"/>
  116. <DataTemplate.Triggers>
  117. <DataTrigger Binding="{Binding IsSelected}" Value="True">
  118. <Setter Property="Background" TargetName="tb" Value="Yellow"/>
  119. </DataTrigger>
  120. </DataTemplate.Triggers>
  121. </DataTemplate>
  122. </DataGridTemplateColumn.CellTemplate>
  123. </DataGridTemplateColumn>
  124. <DataGridTemplateColumn Header="Second">
  125. <DataGridTemplateColumn.CellTemplate>
  126. <DataTemplate DataType="{x:Type local:ParentData}">
  127. <TextBlock Text="{Binding Second.Title}" Name="tb"/>
  128. <DataTemplate.Triggers>
  129. <DataTrigger Binding="{Binding IsSelected}" Value="True">
  130. <Setter Property="Background" TargetName="tb" Value="Green"/>
  131. </DataTrigger>
  132. </DataTemplate.Triggers>
  133. </DataTemplate>
  134. </DataGridTemplateColumn.CellTemplate>
  135. </DataGridTemplateColumn>
  136. </DataGrid.Columns>
  137. </DataGrid>
  138.  
  139. <ItemsControl ItemsSource="{Binding Flat}" Grid.Column="1">
  140. <ItemsControl.ItemTemplate>
  141. <DataTemplate DataType="{x:Type local:Data}">
  142. <TextBlock Text="{Binding Title}" Name="tb"/>
  143. <DataTemplate.Triggers>
  144. <DataTrigger Binding="{Binding IsParentSelected}" Value="True">
  145. <Setter Property="Background" TargetName="tb" Value="Blue"/>
  146. </DataTrigger>
  147. </DataTemplate.Triggers>
  148. </DataTemplate>
  149. </ItemsControl.ItemTemplate>
  150. </ItemsControl>
  151. </Grid>
  152. </Window>
RAW Paste Data