DynamicGridStyle.cs 11 KB

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