You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

179 lines
6.3 KiB

  1. using BPA.UIControl.Commons;
  2. using System;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Xml.Linq;
  6. namespace BPA.UIControl
  7. {
  8. /// <summary>
  9. /// Grid 帮助类
  10. /// </summary>
  11. public static class GridHelper
  12. {
  13. /// <summary>
  14. /// 列数
  15. /// (最后一列填充)
  16. /// </summary>
  17. public static readonly DependencyProperty ColumnsProperty = DependencyProperty.RegisterAttached(
  18. "Columns", typeof(int), typeof(GridHelper), new PropertyMetadata(default(int), OnColumnsChanged));
  19. private static void OnColumnsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  20. {
  21. if (d is Grid grid)
  22. {
  23. grid.ColumnDefinitions.Clear();
  24. var columns = (int)e.NewValue;
  25. for (int i = 0; i < columns; i++)
  26. {
  27. var columnDefinition = new ColumnDefinition();
  28. columnDefinition.Width = i == columns - 1 ? new GridLength(1, GridUnitType.Star) : GridLength.Auto;
  29. grid.ColumnDefinitions.Add(columnDefinition);
  30. }
  31. }
  32. }
  33. /// <summary>
  34. /// Gets the columns.
  35. /// </summary>
  36. /// <param name="obj">The obj.</param>
  37. /// <returns>A double.</returns>
  38. public static int GetColumns(DependencyObject obj)
  39. {
  40. return (int)obj.GetValue(ColumnsProperty);
  41. }
  42. /// <summary>
  43. /// Sets the columns.
  44. /// </summary>
  45. /// <param name="obj">The obj.</param>
  46. /// <param name="value">The value.</param>
  47. public static void SetColumns(DependencyObject obj, int value)
  48. {
  49. obj.SetValue(ColumnsProperty, value);
  50. }
  51. /// <summary>
  52. /// 行数
  53. /// (最后一行填充)
  54. /// </summary>
  55. public static readonly DependencyProperty RowsProperty = DependencyProperty.RegisterAttached(
  56. "Rows", typeof(int), typeof(GridHelper), new PropertyMetadata(default(int), OnRowsChanged));
  57. private static void OnRowsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  58. {
  59. if (d is Grid grid)
  60. {
  61. grid.RowDefinitions.Clear();
  62. var rows = (int)e.NewValue;
  63. for (int i = 0; i < rows; i++)
  64. {
  65. var rowDefinition = new RowDefinition();
  66. rowDefinition.Height = i == rows - 1 ? new GridLength(1, GridUnitType.Star) : GridLength.Auto;
  67. grid.RowDefinitions.Add(rowDefinition);
  68. }
  69. }
  70. }
  71. /// <summary>
  72. /// Gets the rows.
  73. /// </summary>
  74. /// <param name="obj">The obj.</param>
  75. /// <returns>A double.</returns>
  76. public static int GetRows(DependencyObject obj)
  77. {
  78. return (int)obj.GetValue(RowsProperty);
  79. }
  80. /// <summary>
  81. /// Sets the rows.
  82. /// </summary>
  83. /// <param name="obj">The obj.</param>
  84. /// <param name="value">The value.</param>
  85. public static void SetRows(DependencyObject obj, int value)
  86. {
  87. obj.SetValue(RowsProperty, value);
  88. }
  89. /// <summary>
  90. /// 列定义
  91. /// </summary>
  92. public static readonly DependencyProperty ColumnDefinitionsProperty = DependencyProperty.RegisterAttached(
  93. "ColumnDefinitions", typeof(string), typeof(GridHelper), new PropertyMetadata(null, OnColumnDefinitionsChanged));
  94. private static void OnColumnDefinitionsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  95. {
  96. if (d is Grid grid)
  97. {
  98. grid.ColumnDefinitions.Clear();
  99. var converter = new ColumnDefinitionCollectionTypeConverter();
  100. var columnDefinitions = converter.ConvertFromString(GetColumnDefinitions(grid)) as ColumnDefinition[];
  101. for (int i = 0; i < columnDefinitions.Length; i++)
  102. {
  103. grid.ColumnDefinitions.Add(columnDefinitions[i]);
  104. }
  105. }
  106. }
  107. /// <summary>
  108. /// Gets the columns Definitions.
  109. /// </summary>
  110. /// <param name="obj">The obj.</param>
  111. /// <returns>A double.</returns>
  112. public static string GetColumnDefinitions(DependencyObject obj)
  113. {
  114. return (string)obj.GetValue(ColumnDefinitionsProperty);
  115. }
  116. /// <summary>
  117. /// Sets the columns Definitions.
  118. /// </summary>
  119. /// <param name="obj">The obj.</param>
  120. /// <param name="value">The value.</param>
  121. public static void SetColumnDefinitions(DependencyObject obj, string value)
  122. {
  123. obj.SetValue(ColumnDefinitionsProperty, value);
  124. }
  125. /// <summary>
  126. /// 行定义
  127. /// </summary>
  128. public static readonly DependencyProperty RowDefinitionsProperty = DependencyProperty.RegisterAttached(
  129. "RowDefinitions", typeof(string), typeof(GridHelper), new PropertyMetadata(null, OnRowDefinitionsChanged));
  130. private static void OnRowDefinitionsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  131. {
  132. if (d is Grid grid)
  133. {
  134. grid.RowDefinitions.Clear();
  135. var converter = new RowDefinitionCollectionTypeConverter();
  136. var rowDefinitions = converter.ConvertFromString(GetRowDefinitions(grid)) as RowDefinition[];
  137. for (int i = 0; i < rowDefinitions.Length; i++)
  138. {
  139. grid.RowDefinitions.Add(rowDefinitions[i]);
  140. }
  141. }
  142. }
  143. /// <summary>
  144. /// Gets the rows Definitions.
  145. /// </summary>
  146. /// <param name="obj">The obj.</param>
  147. /// <returns>A double.</returns>
  148. public static string GetRowDefinitions(DependencyObject obj)
  149. {
  150. return (string)obj.GetValue(RowDefinitionsProperty);
  151. }
  152. /// <summary>
  153. /// Sets the rows Definitions.
  154. /// </summary>
  155. /// <param name="obj">The obj.</param>
  156. /// <param name="value">The value.</param>
  157. public static void SetRowDefinitions(DependencyObject obj, string value)
  158. {
  159. obj.SetValue(RowDefinitionsProperty, value);
  160. }
  161. }
  162. }