// ----------------------------------------------------------------------- // // Copyright 2015 MIT Licence. See licence.md for more information. // // ----------------------------------------------------------------------- namespace Perspex.Diagnostics.Views { using Perspex.Controls; using Perspex.Controls.Templates; using System; using System.Collections; using System.Collections.Generic; internal static class GridRepeater { public static readonly PerspexProperty ItemsProperty = PerspexProperty.RegisterAttached("Items", typeof(GridRepeater)); public static readonly PerspexProperty>> TemplateProperty = PerspexProperty.RegisterAttached>>("Template", typeof(GridRepeater)); static GridRepeater() { ItemsProperty.Changed.Subscribe(ItemsChanged); } private static void ItemsChanged(PerspexPropertyChangedEventArgs e) { var grid = (Grid)e.Sender; var items = (IEnumerable)e.NewValue; var template = grid.GetValue(TemplateProperty); grid.Children.Clear(); if (items != null) { int count = 0; int cols = grid.ColumnDefinitions.Count; foreach (var item in items) { foreach (var control in template(item)) { grid.Children.Add(control); Grid.SetColumn(control, count % cols); Grid.SetRow(control, count / cols); ++count; } } int rows = (int)Math.Ceiling((double)count / cols); int difference = rows - grid.RowDefinitions.Count; if (difference > 0) { for (int i = 0; i < difference; ++i) { grid.RowDefinitions.Add(new RowDefinition(GridLength.Auto)); } } else if (difference < 0) { for (int i = 0; i < difference; ++i) { grid.RowDefinitions.RemoveAt(grid.RowDefinitions.Count - 1); } } } } } }