Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class YearOfLife
- {
- public int YearAbsolute;
- public int YearRelative;
- public List<Week> Weeks = new List<Week>();
- }
- public class Week
- {
- public string Tooltip;
- public WeekType WeekType;
- public DateTime Start;
- public DateTime End;
- }
- public enum WeekType
- {
- Empty,
- Passed,
- Future,
- PossibleFuture
- }
- List<YearOfLife> Life = new List<YearOfLife>()
- public void ReGenerateLife()
- {
- Life.Clear();
- var yearStart = new DateTime(DayBirth.Year, 1, 1, 0, 0, 0);
- yearStart = yearStart.AddDays(8 - (int)yearStart.DayOfWeek);
- for (var i = yearStart; i <= DayDeath2; i = i.AddYears(1))
- {
- var tmpYear = new YearOfLife();
- tmpYear.YearAbsolute = i.Year;
- tmpYear.YearRelative = i.Year - DayBirth.Year;
- Life.Add(tmpYear);
- }
- var currDate = yearStart;
- while (currDate <= DayDeath2)
- {
- var lstItem = Life.Where(a => a.YearAbsolute == currDate.Year).ToList()[0];
- var week = new Week();
- week.Start = currDate;
- week.End = currDate.AddDays(6);
- if (currDate < DayBirth)
- {
- week.WeekType = WeekType.Empty;
- }
- else if (currDate <= DateTime.Today)
- {
- week.WeekType = WeekType.Passed;
- }
- else if (currDate <= DayDeath)
- {
- week.WeekType = WeekType.Future;
- }
- else if (currDate <= DayDeath2)
- {
- week.WeekType = WeekType.PossibleFuture;
- }
- lstItem.Weeks.Add(week);
- currDate = currDate.AddDays(7);
- }
- }
- <Window.Resources>
- <Style TargetType="ColumnDefinition" x:Key="lifecol" >
- <Setter Property="SharedSizeGroup" Value="A"/>
- <Setter Property="Width" Value="30"/>
- </Style>
- <Style TargetType="RowDefinition" x:Key="liferow" >
- <Setter Property="SharedSizeGroup" Value="A"/>
- <Setter Property="Height" Value="20"/>
- </Style>
- </Window.Resources>
- <ListBox Grid.Row="2" Name="lifeControlWrapper" Background="AliceBlue" ItemsSource="{Binding Life}">
- <ListBox.ItemTemplate>
- <DataTemplate>
- <StackPanel Orientation="Horizontal">
- <Grid>
- <Grid.ColumnDefinitions>
- <ColumnDefinition/>
- <ColumnDefinition/>
- <ColumnDefinition/>
- </Grid.ColumnDefinitions>
- <Grid.RowDefinitions>
- <RowDefinition Height="20"/>
- </Grid.RowDefinitions>
- <TextBlock Text="{Binding YearsAbsolute}" VerticalAlignment="Center" Grid.Column="0"/>
- <TextBlock Text="{Binding YearsRelative}" VerticalAlignment="Center" HorizontalAlignment="Right" Grid.Column="1"/>
- <Grid Grid.Column="2" HorizontalAlignment="Stretch" Grid.IsSharedSizeScope="True">
- <Grid.ColumnDefinitions>
- <ColumnDefinition Style="{StaticResource lifecol}"/>
- </Grid.ColumnDefinitions>
- <Grid.RowDefinitions>
- <RowDefinition Style="{StaticResource liferow}"/>
- </Grid.RowDefinitions>
- <Button Name="brnFirst" Width="auto" Margin="2" Grid.Column="0" Height="{Binding ElementName=brnFirst, Path=Width}"></Button>
- <Button Width="auto" Margin="2" Grid.Column="1"></Button>
- </Grid>
- </Grid>
- </StackPanel>
- </DataTemplate>
- </ListBox.ItemTemplate>
- </ListBox>
- public class SplitGrid : Panel
- {
- public int Columns
- {
- get => (int)GetValue(ColumnsProperty);
- set => SetValue(ColumnsProperty, value);
- }
- public static readonly DependencyProperty ColumnsProperty =
- DependencyProperty.Register(nameof(Columns), typeof(int), typeof(SplitGrid),
- new FrameworkPropertyMetadata(1, FrameworkPropertyMetadataOptions.AffectsMeasure), ValidateColumns);
- private static bool ValidateColumns(object value) => (int)value > 0;
- public double ShortSpace
- {
- get => (double)GetValue(ShortSpaceProperty);
- set => SetValue(ShortSpaceProperty, value);
- }
- public static readonly DependencyProperty ShortSpaceProperty =
- DependencyProperty.Register(nameof(ShortSpace), typeof(double), typeof(SplitGrid),
- new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.AffectsMeasure));
- public double LongSpace
- {
- get => (double)GetValue(LongSpaceProperty);
- set => SetValue(LongSpaceProperty, value);
- }
- public static readonly DependencyProperty LongSpaceProperty =
- DependencyProperty.Register(nameof(LongSpace), typeof(double), typeof(SplitGrid),
- new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.AffectsMeasure));
- public int LongSpacePeriod
- {
- get { return (int)GetValue(LongSpacePeriodProperty); }
- set { SetValue(LongSpacePeriodProperty, value); }
- }
- public static readonly DependencyProperty LongSpacePeriodProperty =
- DependencyProperty.Register(nameof(LongSpacePeriod), typeof(int), typeof(SplitGrid),
- new FrameworkPropertyMetadata(5, FrameworkPropertyMetadataOptions.AffectsMeasure), ValidateLongSpacePeriod);
- private static bool ValidateLongSpacePeriod(object value) => (int)value > 0;
- // Этап подсчета занимаемого места
- protected override Size MeasureOverride(Size constraint)
- {
- columns = Columns;
- longSpacePeriod = LongSpacePeriod;
- longSpace = LongSpace;
- shortSpace = ShortSpace;
- // Общая длина отступов
- totalSpace = CalcTotalSpace(columns - 1);
- double childLength = (constraint.Width - totalSpace) / columns;
- if (childLength < 0) childLength = 0;
- var childConstraint = new Size(childLength, childLength);
- int items = 0;
- foreach (UIElement child in InternalChildren)
- {
- // Обязанность панели запросить желаемое место, даже если она не будет этим пользоваться
- child.Measure(childConstraint);
- if (child.Visibility != Visibility.Collapsed) ++items;
- }
- // Итоговая желаемая высота панели
- int rows = (columns + items - 1) / columns;
- double height = CalcTotalSpace(rows - 1) + rows * childLength;
- return new Size(constraint.Width, height);
- }
- // Этап размещения элементов
- protected override Size ArrangeOverride(Size arrangeSize)
- {
- double childLength = (arrangeSize.Width - totalSpace) / columns;
- if (childLength < 0) childLength = 0;
- int items = 0;
- foreach (UIElement child in InternalChildren)
- {
- int column = items % columns;
- int row = items / columns;
- double x = column * childLength + CalcTotalSpace(column);
- double y = row * childLength + CalcTotalSpace(row);
- var childBounds = new Rect(x, y, childLength, childLength);
- // Размещаем
- child.Arrange(childBounds);
- if (child.Visibility != Visibility.Collapsed) ++items;
- }
- int rows = (columns + items - 1) / columns;
- double height = CalcTotalSpace(rows - 1) + rows * childLength;
- return new Size(arrangeSize.Width, height);
- }
- private int columns;
- private int longSpacePeriod;
- private double longSpace;
- private double shortSpace;
- private double totalSpace;
- private double CalcTotalSpace(int totalNum)
- {
- if (totalNum < 0) return 0;
- int longNum = totalNum / longSpacePeriod;
- int shortNum = totalNum - longNum;
- return longNum * longSpace + shortNum * shortSpace;
- }
- }
- class MainVm : Vm
- {
- public List<int> Items { get; } = Enumerable.Range(0, 53 * 10).ToList();
- }
- <Grid Margin="5">
- <ScrollViewer VerticalScrollBarVisibility="Auto">
- <ItemsControl ItemsSource="{Binding Items}" VerticalAlignment="Top">
- <ItemsControl.ItemsPanel>
- <ItemsPanelTemplate>
- <c:SplitGrid Columns="53" ShortSpace="2" LongSpace="5" LongSpacePeriod="5"/>
- </ItemsPanelTemplate>
- </ItemsControl.ItemsPanel>
- <ItemsControl.ItemTemplate>
- <DataTemplate>
- <Border BorderBrush="Gray" BorderThickness="1"/>
- </DataTemplate>
- </ItemsControl.ItemTemplate>
- </ItemsControl>
- </ScrollViewer>
- </Grid>
Add Comment
Please, Sign In to add comment