Guest User

Untitled

a guest
Jan 16th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.59 KB | None | 0 0
  1. public class YearOfLife
  2. {
  3. public int YearAbsolute;
  4. public int YearRelative;
  5.  
  6. public List<Week> Weeks = new List<Week>();
  7. }
  8.  
  9. public class Week
  10. {
  11. public string Tooltip;
  12.  
  13. public WeekType WeekType;
  14.  
  15. public DateTime Start;
  16. public DateTime End;
  17. }
  18.  
  19. public enum WeekType
  20. {
  21. Empty,
  22. Passed,
  23. Future,
  24. PossibleFuture
  25. }
  26.  
  27. List<YearOfLife> Life = new List<YearOfLife>()
  28.  
  29. public void ReGenerateLife()
  30. {
  31. Life.Clear();
  32.  
  33. var yearStart = new DateTime(DayBirth.Year, 1, 1, 0, 0, 0);
  34. yearStart = yearStart.AddDays(8 - (int)yearStart.DayOfWeek);
  35.  
  36. for (var i = yearStart; i <= DayDeath2; i = i.AddYears(1))
  37. {
  38. var tmpYear = new YearOfLife();
  39. tmpYear.YearAbsolute = i.Year;
  40. tmpYear.YearRelative = i.Year - DayBirth.Year;
  41.  
  42. Life.Add(tmpYear);
  43. }
  44.  
  45. var currDate = yearStart;
  46.  
  47. while (currDate <= DayDeath2)
  48. {
  49. var lstItem = Life.Where(a => a.YearAbsolute == currDate.Year).ToList()[0];
  50.  
  51. var week = new Week();
  52. week.Start = currDate;
  53. week.End = currDate.AddDays(6);
  54.  
  55. if (currDate < DayBirth)
  56. {
  57. week.WeekType = WeekType.Empty;
  58. }
  59. else if (currDate <= DateTime.Today)
  60. {
  61. week.WeekType = WeekType.Passed;
  62. }
  63. else if (currDate <= DayDeath)
  64. {
  65. week.WeekType = WeekType.Future;
  66. }
  67. else if (currDate <= DayDeath2)
  68. {
  69. week.WeekType = WeekType.PossibleFuture;
  70. }
  71.  
  72. lstItem.Weeks.Add(week);
  73. currDate = currDate.AddDays(7);
  74. }
  75. }
  76.  
  77. <Window.Resources>
  78. <Style TargetType="ColumnDefinition" x:Key="lifecol" >
  79. <Setter Property="SharedSizeGroup" Value="A"/>
  80. <Setter Property="Width" Value="30"/>
  81. </Style>
  82. <Style TargetType="RowDefinition" x:Key="liferow" >
  83. <Setter Property="SharedSizeGroup" Value="A"/>
  84. <Setter Property="Height" Value="20"/>
  85. </Style>
  86. </Window.Resources>
  87.  
  88. <ListBox Grid.Row="2" Name="lifeControlWrapper" Background="AliceBlue" ItemsSource="{Binding Life}">
  89. <ListBox.ItemTemplate>
  90. <DataTemplate>
  91. <StackPanel Orientation="Horizontal">
  92. <Grid>
  93. <Grid.ColumnDefinitions>
  94. <ColumnDefinition/>
  95. <ColumnDefinition/>
  96. <ColumnDefinition/>
  97. </Grid.ColumnDefinitions>
  98. <Grid.RowDefinitions>
  99. <RowDefinition Height="20"/>
  100. </Grid.RowDefinitions>
  101. <TextBlock Text="{Binding YearsAbsolute}" VerticalAlignment="Center" Grid.Column="0"/>
  102. <TextBlock Text="{Binding YearsRelative}" VerticalAlignment="Center" HorizontalAlignment="Right" Grid.Column="1"/>
  103. <Grid Grid.Column="2" HorizontalAlignment="Stretch" Grid.IsSharedSizeScope="True">
  104. <Grid.ColumnDefinitions>
  105. <ColumnDefinition Style="{StaticResource lifecol}"/>
  106. </Grid.ColumnDefinitions>
  107. <Grid.RowDefinitions>
  108. <RowDefinition Style="{StaticResource liferow}"/>
  109. </Grid.RowDefinitions>
  110. <Button Name="brnFirst" Width="auto" Margin="2" Grid.Column="0" Height="{Binding ElementName=brnFirst, Path=Width}"></Button>
  111. <Button Width="auto" Margin="2" Grid.Column="1"></Button>
  112. </Grid>
  113. </Grid>
  114. </StackPanel>
  115. </DataTemplate>
  116. </ListBox.ItemTemplate>
  117. </ListBox>
  118.  
  119. public class SplitGrid : Panel
  120. {
  121. public int Columns
  122. {
  123. get => (int)GetValue(ColumnsProperty);
  124. set => SetValue(ColumnsProperty, value);
  125. }
  126.  
  127. public static readonly DependencyProperty ColumnsProperty =
  128. DependencyProperty.Register(nameof(Columns), typeof(int), typeof(SplitGrid),
  129. new FrameworkPropertyMetadata(1, FrameworkPropertyMetadataOptions.AffectsMeasure), ValidateColumns);
  130.  
  131. private static bool ValidateColumns(object value) => (int)value > 0;
  132.  
  133. public double ShortSpace
  134. {
  135. get => (double)GetValue(ShortSpaceProperty);
  136. set => SetValue(ShortSpaceProperty, value);
  137. }
  138.  
  139. public static readonly DependencyProperty ShortSpaceProperty =
  140. DependencyProperty.Register(nameof(ShortSpace), typeof(double), typeof(SplitGrid),
  141. new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.AffectsMeasure));
  142.  
  143. public double LongSpace
  144. {
  145. get => (double)GetValue(LongSpaceProperty);
  146. set => SetValue(LongSpaceProperty, value);
  147. }
  148.  
  149. public static readonly DependencyProperty LongSpaceProperty =
  150. DependencyProperty.Register(nameof(LongSpace), typeof(double), typeof(SplitGrid),
  151. new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.AffectsMeasure));
  152.  
  153. public int LongSpacePeriod
  154. {
  155. get { return (int)GetValue(LongSpacePeriodProperty); }
  156. set { SetValue(LongSpacePeriodProperty, value); }
  157. }
  158.  
  159. public static readonly DependencyProperty LongSpacePeriodProperty =
  160. DependencyProperty.Register(nameof(LongSpacePeriod), typeof(int), typeof(SplitGrid),
  161. new FrameworkPropertyMetadata(5, FrameworkPropertyMetadataOptions.AffectsMeasure), ValidateLongSpacePeriod);
  162.  
  163. private static bool ValidateLongSpacePeriod(object value) => (int)value > 0;
  164.  
  165. // Этап подсчета занимаемого места
  166. protected override Size MeasureOverride(Size constraint)
  167. {
  168. columns = Columns;
  169. longSpacePeriod = LongSpacePeriod;
  170. longSpace = LongSpace;
  171. shortSpace = ShortSpace;
  172.  
  173. // Общая длина отступов
  174. totalSpace = CalcTotalSpace(columns - 1);
  175.  
  176. double childLength = (constraint.Width - totalSpace) / columns;
  177. if (childLength < 0) childLength = 0;
  178. var childConstraint = new Size(childLength, childLength);
  179.  
  180. int items = 0;
  181. foreach (UIElement child in InternalChildren)
  182. {
  183. // Обязанность панели запросить желаемое место, даже если она не будет этим пользоваться
  184. child.Measure(childConstraint);
  185. if (child.Visibility != Visibility.Collapsed) ++items;
  186. }
  187.  
  188. // Итоговая желаемая высота панели
  189. int rows = (columns + items - 1) / columns;
  190. double height = CalcTotalSpace(rows - 1) + rows * childLength;
  191. return new Size(constraint.Width, height);
  192. }
  193.  
  194. // Этап размещения элементов
  195. protected override Size ArrangeOverride(Size arrangeSize)
  196. {
  197. double childLength = (arrangeSize.Width - totalSpace) / columns;
  198. if (childLength < 0) childLength = 0;
  199.  
  200. int items = 0;
  201. foreach (UIElement child in InternalChildren)
  202. {
  203. int column = items % columns;
  204. int row = items / columns;
  205. double x = column * childLength + CalcTotalSpace(column);
  206. double y = row * childLength + CalcTotalSpace(row);
  207. var childBounds = new Rect(x, y, childLength, childLength);
  208. // Размещаем
  209. child.Arrange(childBounds);
  210. if (child.Visibility != Visibility.Collapsed) ++items;
  211. }
  212.  
  213. int rows = (columns + items - 1) / columns;
  214. double height = CalcTotalSpace(rows - 1) + rows * childLength;
  215. return new Size(arrangeSize.Width, height);
  216. }
  217.  
  218. private int columns;
  219. private int longSpacePeriod;
  220. private double longSpace;
  221. private double shortSpace;
  222. private double totalSpace;
  223.  
  224. private double CalcTotalSpace(int totalNum)
  225. {
  226. if (totalNum < 0) return 0;
  227. int longNum = totalNum / longSpacePeriod;
  228. int shortNum = totalNum - longNum;
  229. return longNum * longSpace + shortNum * shortSpace;
  230. }
  231. }
  232.  
  233. class MainVm : Vm
  234. {
  235. public List<int> Items { get; } = Enumerable.Range(0, 53 * 10).ToList();
  236. }
  237.  
  238. <Grid Margin="5">
  239. <ScrollViewer VerticalScrollBarVisibility="Auto">
  240. <ItemsControl ItemsSource="{Binding Items}" VerticalAlignment="Top">
  241. <ItemsControl.ItemsPanel>
  242. <ItemsPanelTemplate>
  243. <c:SplitGrid Columns="53" ShortSpace="2" LongSpace="5" LongSpacePeriod="5"/>
  244. </ItemsPanelTemplate>
  245. </ItemsControl.ItemsPanel>
  246. <ItemsControl.ItemTemplate>
  247. <DataTemplate>
  248. <Border BorderBrush="Gray" BorderThickness="1"/>
  249. </DataTemplate>
  250. </ItemsControl.ItemTemplate>
  251. </ItemsControl>
  252. </ScrollViewer>
  253. </Grid>
Add Comment
Please, Sign In to add comment