Advertisement
Guest User

Untitled

a guest
Dec 2nd, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.07 KB | None | 0 0
  1.     public static class GridEx
  2.     {
  3.         public static int GetRowCount(Grid obj) => (int)obj.GetValue(RowCountProperty);
  4.         public static void SetRowCount(Grid obj, int value) => obj.SetValue(RowCountProperty, value);
  5.  
  6.         public static readonly DependencyProperty RowCountProperty =
  7.             DependencyProperty.RegisterAttached("RowCount", typeof(int), typeof(GridEx), new PropertyMetadata(0, OnRowsChanged));
  8.  
  9.         public static string GetRowHeights(Grid obj) => (string)obj.GetValue(RowHeightsProperty);
  10.         public static void SetRowHeights(Grid obj, string value) => obj.SetValue(RowHeightsProperty, value);
  11.  
  12.         public static readonly DependencyProperty RowHeightsProperty =
  13.             DependencyProperty.RegisterAttached("RowHeights", typeof(string), typeof(GridEx), new PropertyMetadata("", OnRowsChanged));
  14.  
  15.         private static void OnRowsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  16.         {
  17.             var grid = (Grid)d;
  18.             var rowDefinitions = grid.RowDefinitions;
  19.             var rowCount = GetRowCount(grid);
  20.             var rowHeightsString = GetRowHeights(grid);
  21.             var rowHeights = ParseRowHeightsString(rowHeightsString);
  22.             var rowCountTotal = Math.Max(rowCount, rowHeights.Length);
  23.             if (rowDefinitions.Count > rowCountTotal)
  24.                 rowDefinitions.RemoveRange(rowCount, rowDefinitions.Count - rowCountTotal);
  25.             for (int i = 0; i < rowHeights.Length; ++i)
  26.             {
  27.                 if (rowDefinitions.Count <= i)
  28.                     rowDefinitions.Add(new RowDefinition { Height = rowHeights[i] });
  29.                 if (rowDefinitions[i].Height != rowHeights[i])
  30.                     rowDefinitions[i].Height = rowHeights[i];
  31.             }
  32.             while (rowDefinitions.Count < rowCount)
  33.                 rowDefinitions.Add(new RowDefinition());
  34.         }
  35.  
  36.         private static GridLength[] ParseRowHeightsString(string rowHeightsString)
  37.         {
  38.             var parts = rowHeightsString.ToUpper()
  39.                             .Split(" ,".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
  40.             var gridLengths = new GridLength[parts.Length];
  41.             for (int i = 0; i < parts.Length; ++i)
  42.             {
  43.                 if (parts[i] == "AUTO") continue;
  44.                 int endSymbolIndex = parts[i].Length - 1;
  45.                 if (parts[i][endSymbolIndex] == '*')
  46.                 {
  47.                     double value = 1;
  48.                     if (parts[i].Length > 1
  49.                         && !double.TryParse(parts[i].Substring(0, endSymbolIndex), out value))
  50.                             throw new ArgumentException("Illegal argument RowHeights");
  51.                     gridLengths[i] = new GridLength(value, GridUnitType.Star);
  52.                     continue;
  53.                 }
  54.                 if (!double.TryParse(parts[i], out var pixels))
  55.                     throw new ArgumentException("Illegal argument RowHeights");
  56.                 gridLengths[i] = new GridLength(pixels);
  57.             }
  58.             return gridLengths;
  59.         }
  60.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement