DynamicGridStyle.cs 10 KB

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