Advertisement
FrayxRulez

FluidWrapGrid taked somewhere

Sep 19th, 2014
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.12 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Windows.Foundation;
  7. using Windows.UI.Xaml;
  8. using Windows.UI.Xaml.Controls;
  9.  
  10. namespace DMAX.Controls
  11. {
  12.     /// <summary>
  13.     /// A UniformGrid implementation for WinRT that arranges children in a
  14.     /// grid with all equal cell sizes.
  15.     /// </summary>
  16.     public class FluidWrapGrid : Panel
  17.     {
  18.         /// <summary>
  19.         /// Gets or sets the computed row value.
  20.         /// </summary>
  21.         private int ComputedRows { get; set; }
  22.  
  23.         /// <summary>
  24.         /// Gets or sets the computed column value.
  25.         /// </summary>
  26.         private int ComputedColumns { get; set; }
  27.  
  28.         /// <summary>
  29.         /// Initializes a new instance of FluidWrapGrid.
  30.         /// </summary>
  31.         public FluidWrapGrid()
  32.         {
  33.         }
  34.  
  35.         /// <summary>
  36.         /// Gets or sets the number of first columns to leave blank.
  37.         /// </summary>
  38.         public int FirstColumn
  39.         {
  40.             get { return (int)GetValue(FirstColumnProperty); }
  41.             set { SetValue(FirstColumnProperty, value); }
  42.         }
  43.  
  44.         /// <summary>
  45.         /// The FirstColumnProperty dependency property.
  46.         /// </summary>
  47.         public static readonly DependencyProperty FirstColumnProperty =
  48.                 DependencyProperty.Register(
  49.                         "FirstColumn",
  50.                         typeof(int),
  51.                         typeof(FluidWrapGrid),
  52.                         new PropertyMetadata(0, OnIntegerDependencyPropertyChanged));
  53.  
  54.         /// <summary>
  55.         /// Gets or sets the number of columns in the grid. A value of zero
  56.         /// indicates that the count should be dynamically computed based on the
  57.         /// number of rows and the number of non-collapsed children in the grid.
  58.         /// </summary>
  59.         public int Columns
  60.         {
  61.             get { return (int)GetValue(ColumnsProperty); }
  62.             set { SetValue(ColumnsProperty, value); }
  63.         }
  64.  
  65.         /// <summary>
  66.         /// DependencyProperty for the Columns property.
  67.         /// </summary>
  68.         public static readonly DependencyProperty ColumnsProperty =
  69.                 DependencyProperty.Register(
  70.                         "Columns",
  71.                         typeof(int),
  72.                         typeof(FluidWrapGrid),
  73.                         new PropertyMetadata(0, OnIntegerDependencyPropertyChanged));
  74.  
  75.         /// <summary>
  76.         /// Validate the new property value and silently revert if the new value
  77.         /// is not appropriate. Used in place of WPF value coercian by the
  78.         /// dependency properties in FluidWrapGrid.
  79.         /// </summary>
  80.         /// <param name="o">The dependency object.</param>
  81.         /// <param name="e">The dependency property.</param>
  82.         private static void OnIntegerDependencyPropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
  83.         {
  84.             // Silently coerce the value back to >= 0 if negative.
  85.             if (!(e.NewValue is int) || (int)e.NewValue < 0)
  86.             {
  87.                 o.SetValue(e.Property, e.OldValue);
  88.             }
  89.         }
  90.  
  91.         /// <summary>
  92.         /// Gets or sets the number of rows in the grid. A value of zero
  93.         /// indicates that the row count should be dynamically computed based on
  94.         /// the number of columns and the number of non-collapsed children in
  95.         /// the grid.
  96.         /// </summary>
  97.         public int Rows
  98.         {
  99.             get { return (int)GetValue(RowsProperty); }
  100.             set { SetValue(RowsProperty, value); }
  101.         }
  102.  
  103.         /// <summary>
  104.         /// The Rows DependencyProperty.
  105.         /// </summary>
  106.         public static readonly DependencyProperty RowsProperty =
  107.                 DependencyProperty.Register(
  108.                         "Rows",
  109.                         typeof(int),
  110.                         typeof(FluidWrapGrid),
  111.                         new PropertyMetadata(0, OnIntegerDependencyPropertyChanged));
  112.  
  113.         /// <summary>
  114.         /// Compute the desired size of the FluidWrapGrid by measuring all of the
  115.         /// children with a constraint equal to a cell's portion of the given
  116.         /// constraint. The maximum child width and maximum child height are
  117.         /// tracked, and then the desired size is computed by multiplying these
  118.         /// maximums by the row and column count.
  119.         /// </summary>
  120.         /// <param name="constraint">The size constraint.</param>
  121.         /// <returns>Returns the desired size.</returns>
  122.         protected override Size MeasureOverride(Size constraint)
  123.         {
  124.             UpdateComputedValues();
  125.  
  126.             Size childConstraint = new Size(constraint.Width / ComputedColumns, constraint.Height / ComputedRows);
  127.             double maxChildDesiredWidth = 0.0;
  128.             double maxChildDesiredHeight = 0.0;
  129.  
  130.             //  Measure each child, keeping track of max desired width & height.
  131.             for (int i = 0, count = Children.Count; i < count; ++i)
  132.             {
  133.                 UIElement child = Children[i];
  134.                 child.Measure(childConstraint);
  135.                 Size childDesiredSize = child.DesiredSize;
  136.                 if (maxChildDesiredWidth < childDesiredSize.Width)
  137.                 {
  138.                     maxChildDesiredWidth = childDesiredSize.Width;
  139.                 }
  140.                 if (maxChildDesiredHeight < childDesiredSize.Height)
  141.                 {
  142.                     maxChildDesiredHeight = childDesiredSize.Height;
  143.                 }
  144.             }
  145.             return new Size((maxChildDesiredWidth * ComputedColumns), (maxChildDesiredHeight * ComputedRows));
  146.         }
  147.  
  148.         /// <summary>
  149.         /// Arrange the children of the FluidWrapGrid by distributing space evenly
  150.         /// among the children, making each child the size equal to a cell
  151.         /// portion of the arrangeSize parameter.
  152.         /// </summary>
  153.         /// <param name="arrangeSize">The arrange size.</param>
  154.         /// <returns>Returns the updated Size.</returns>
  155.         protected override Size ArrangeOverride(Size arrangeSize)
  156.         {
  157.             Rect childBounds = new Rect(0, 0, arrangeSize.Width / ComputedColumns, arrangeSize.Height / ComputedRows);
  158.             double xStep = childBounds.Width;
  159.             double xBound = arrangeSize.Width - 1.0;
  160.             childBounds.X += childBounds.Width * FirstColumn;
  161.  
  162.             // Arrange and Position each child to the same cell size
  163.             foreach (UIElement child in Children)
  164.             {
  165.                 child.Arrange(childBounds);
  166.                 if (child.Visibility != Visibility.Collapsed)
  167.                 {
  168.                     childBounds.X += xStep;
  169.                     if (childBounds.X >= xBound)
  170.                     {
  171.                         childBounds.Y += childBounds.Height;
  172.                         childBounds.X = 0;
  173.                     }
  174.                 }
  175.             }
  176.  
  177.             return arrangeSize;
  178.         }
  179.  
  180.         /// <summary>
  181.         /// If the Rows or Columns values are set to 0, dynamically compute the
  182.         /// values based on the actual number of non-collapsed children.
  183.         /// </summary>
  184.         /// <remarks>
  185.         /// In the case when both Rows and Columns are set to 0, the Rows and
  186.         /// Columns will be equal, laying out a square grid.
  187.         /// </remarks>
  188.         private void UpdateComputedValues()
  189.         {
  190.             ComputedColumns = Columns;
  191.             ComputedRows = Rows;
  192.  
  193.             // Reset the first column. This is the same logic performed by WPF.
  194.             if (FirstColumn >= ComputedColumns)
  195.             {
  196.                 FirstColumn = 0;
  197.             }
  198.  
  199.             if ((ComputedRows == 0) || (ComputedColumns == 0))
  200.             {
  201.                 int nonCollapsedCount = 0;
  202.                 for (int i = 0, count = Children.Count; i < count; ++i)
  203.                 {
  204.                     UIElement child = Children[i];
  205.                     if (child.Visibility != Visibility.Collapsed)
  206.                     {
  207.                         nonCollapsedCount++;
  208.                     }
  209.                 }
  210.                 if (nonCollapsedCount == 0)
  211.                 {
  212.                     nonCollapsedCount = 1;
  213.                 }
  214.                 if (ComputedRows == 0)
  215.                 {
  216.                     if (ComputedColumns > 0)
  217.                     {
  218.                         ComputedRows = (nonCollapsedCount + FirstColumn + (ComputedColumns - 1)) / ComputedColumns;
  219.                     }
  220.                     else
  221.                     {
  222.                         ComputedRows = (int)Math.Sqrt(nonCollapsedCount);
  223.                         if ((ComputedRows * ComputedRows) < nonCollapsedCount)
  224.                         {
  225.                             ComputedRows++;
  226.                         }
  227.                         ComputedColumns = ComputedRows;
  228.                     }
  229.                 }
  230.                 else if (ComputedColumns == 0)
  231.                 {
  232.                     ComputedColumns = (nonCollapsedCount + (ComputedRows - 1)) / ComputedRows;
  233.                 }
  234.             }
  235.         }
  236.     }
  237. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement