DynamicGridStyle.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. using System;
  2. using System.Data;
  3. using System.Linq;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. using System.Windows.Media;
  7. using InABox.Clients;
  8. using InABox.Core;
  9. using InABox.Scripting;
  10. using Syncfusion.UI.Xaml.Grid;
  11. namespace InABox.DynamicGrid
  12. {
  13. public delegate DynamicGridStyle OnGetDynamicGridStyle(CoreRow row, DynamicGridStyle defaultstyle);
  14. public interface IBaseDynamicGridStyle
  15. {
  16. double FontSize { get; set; }
  17. FontStyle FontStyle { get; set; }
  18. FontWeight FontWeight { get; set; }
  19. Brush Background { get; set; }
  20. Brush Foreground { get; set; }
  21. }
  22. public interface IDynamicGridStyle : IBaseDynamicGridStyle
  23. {
  24. DependencyProperty FontSizeProperty { get; }
  25. DependencyProperty FontStyleProperty { get; }
  26. DependencyProperty FontWeightProperty { get; }
  27. DependencyProperty BackgroundProperty { get; }
  28. DependencyProperty ForegroundProperty { get; }
  29. }
  30. public abstract class DynamicGridStyle : Style, IDynamicGridStyle
  31. {
  32. public DynamicGridStyle(Type T) : base(T)
  33. {
  34. FontSize = 12D;
  35. FontWeight = FontWeights.Normal;
  36. FontStyle = FontStyles.Normal;
  37. Background = new SolidColorBrush(Colors.White);
  38. Foreground = new SolidColorBrush(Colors.Black);
  39. }
  40. public DynamicGridStyle(DynamicGridStyle source) : base(source.TargetType, source)
  41. {
  42. FontSize = source.FontSize;
  43. FontWeight = source.FontWeight;
  44. FontStyle = source.FontStyle;
  45. Background = source.Background;
  46. Foreground = source.Foreground;
  47. }
  48. public abstract DependencyProperty FontSizeProperty { get; }
  49. public abstract DependencyProperty FontStyleProperty { get; }
  50. public abstract DependencyProperty FontWeightProperty { get; }
  51. public abstract DependencyProperty BackgroundProperty { get; }
  52. public abstract DependencyProperty ForegroundProperty { get; }
  53. public double FontSize
  54. {
  55. get => Get<double>(FontSizeProperty, 8);
  56. set => Set(FontSizeProperty, value);
  57. }
  58. public FontStyle FontStyle
  59. {
  60. get => Get(FontStyleProperty, FontStyles.Italic);
  61. set => Set(FontStyleProperty, value);
  62. }
  63. public FontWeight FontWeight
  64. {
  65. get => Get(FontWeightProperty, FontWeights.DemiBold);
  66. set => Set(FontWeightProperty, value);
  67. }
  68. public Brush Background
  69. {
  70. get => Get<Brush>(BackgroundProperty, new SolidColorBrush(Colors.Yellow));
  71. set => Set(BackgroundProperty, value);
  72. }
  73. public Brush Foreground
  74. {
  75. get => Get<Brush>(ForegroundProperty, new SolidColorBrush(Colors.Green));
  76. set => Set(ForegroundProperty, value);
  77. }
  78. private void Set<T>(DependencyProperty property, T value)
  79. {
  80. var setter = Setters.FirstOrDefault(x => x is Setter && ((Setter)x).Property.Equals(property)) as Setter;
  81. if (setter == null)
  82. {
  83. Setters.Add(new Setter(property, value));
  84. }
  85. else
  86. {
  87. if (!setter.IsSealed)
  88. setter.Value = value;
  89. }
  90. }
  91. private T Get<T>(DependencyProperty property, T defaultvalue)
  92. {
  93. var setter = Setters.FirstOrDefault(x => x is Setter && ((Setter)x).Property.Equals(property)) as Setter;
  94. return setter != null ? (T)setter.Value : defaultvalue;
  95. }
  96. }
  97. public abstract class DynamicGridStyle<T> : DynamicGridStyle
  98. {
  99. public DynamicGridStyle(IDynamicGridStyle? source) : base(typeof(T))
  100. {
  101. if (source != null)
  102. {
  103. FontSize = source.FontSize;
  104. FontStyle = source.FontStyle;
  105. FontWeight = source.FontWeight;
  106. Background = source.Background;
  107. Foreground = source.Foreground;
  108. }
  109. }
  110. }
  111. public abstract class DynamicGridStyleSelector<T> : StyleSelector, IBaseDynamicGridStyle
  112. {
  113. private static bool initialized;
  114. private static ScriptDocument helper;
  115. private static DynamicGridStyle defaultstyle;
  116. public CoreTable Data { get; set; }
  117. public double FontSize
  118. {
  119. get => defaultstyle.FontSize;
  120. set => defaultstyle.FontSize = value;
  121. }
  122. public FontStyle FontStyle
  123. {
  124. get => defaultstyle.FontStyle;
  125. set => defaultstyle.FontStyle = value;
  126. }
  127. public FontWeight FontWeight
  128. {
  129. get => defaultstyle.FontWeight;
  130. set => defaultstyle.FontWeight = value;
  131. }
  132. public Brush Background
  133. {
  134. get => defaultstyle.Background;
  135. set => defaultstyle.Background = value;
  136. }
  137. public Brush Foreground
  138. {
  139. get => defaultstyle.Foreground;
  140. set => defaultstyle.Foreground = value;
  141. }
  142. protected abstract DynamicGridStyle CreateStyle();
  143. private CoreRow GetRow(object item)
  144. {
  145. try
  146. {
  147. var row = (item as DataRowBase).RowData as DataRowView;
  148. if (row != null)
  149. {
  150. var index = row.Row.Table.Rows.IndexOf(row.Row);
  151. return Data.Rows[index];
  152. }
  153. return null;
  154. }
  155. catch
  156. {
  157. return null;
  158. }
  159. }
  160. private void Initialize()
  161. {
  162. if (initialized)
  163. return;
  164. defaultstyle = CreateStyle();
  165. var stylescript = ClientFactory.GetClientType() == null
  166. ? null
  167. : new Client<Script>()
  168. .Load(new Filter<Script>(x => x.Section).IsEqualTo(typeof(T).EntityName()).And(x => x.ScriptType).IsEqualTo(ScriptType.RowStyle))
  169. .FirstOrDefault();
  170. if (stylescript != null)
  171. {
  172. helper = new ScriptDocument(stylescript.Code);
  173. if (!helper.Compile())
  174. helper = null;
  175. }
  176. initialized = true;
  177. }
  178. public event OnGetDynamicGridStyle GetStyle;
  179. private DynamicGridStyle ExecuteHelper(CoreRow row, DynamicGridStyle style)
  180. {
  181. var result = style;
  182. try
  183. {
  184. helper.SetValue("Row", row);
  185. helper.SetValue("Background", style.Background);
  186. helper.SetValue("Foreground", style.Foreground);
  187. helper.SetValue("Style", style.FontStyle);
  188. helper.SetValue("Weight", style.FontWeight);
  189. helper.SetValue("Size", style.FontSize);
  190. if (helper.Execute())
  191. {
  192. result = CreateStyle();
  193. result.Background = (Brush)helper.GetValue("Background");
  194. result.Foreground = (Brush)helper.GetValue("Foreground");
  195. result.FontStyle = (FontStyle)helper.GetValue("Style");
  196. result.FontWeight = (FontWeight)helper.GetValue("Weight");
  197. result.FontSize = (double)helper.GetValue("Size");
  198. }
  199. }
  200. catch (Exception e)
  201. {
  202. //MessageBox.Show("Unable to Invoke Row Style Helper!\r\n\r\n" + e.Message);
  203. }
  204. return result;
  205. }
  206. public override Style SelectStyle(object item, DependencyObject container)
  207. {
  208. Initialize();
  209. if (Data == null)
  210. return defaultstyle;
  211. if (item == null)
  212. return defaultstyle;
  213. var row = GetRow(item);
  214. if (row == null)
  215. return defaultstyle;
  216. var style = GetStyle != null ? GetStyle(row, defaultstyle) : defaultstyle;
  217. if (helper != null)
  218. return ExecuteHelper(row, style);
  219. return style;
  220. }
  221. }
  222. public class DynamicGridStyleSelector<TEntity, TStyle> : DynamicGridStyleSelector<TEntity> where TStyle : DynamicGridStyle, new()
  223. {
  224. protected override DynamicGridStyle CreateStyle()
  225. {
  226. return new TStyle();
  227. }
  228. }
  229. }