DynamicSplitPanel.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Configuration;
  4. using System.Linq;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Input;
  9. using InABox.Core;
  10. using Syncfusion.Windows.Controls.Input;
  11. namespace InABox.DynamicGrid;
  12. [Flags]
  13. public enum DynamicSplitPanelView
  14. {
  15. Master = 1,
  16. Detail = 2,
  17. Combined = 4
  18. }
  19. public enum DynamicSplitPanelAnchor
  20. {
  21. Master,
  22. Detail
  23. }
  24. public class DynamicSplitPanelSettings : EventArgs
  25. {
  26. public DynamicSplitPanelView View { get; set; }
  27. public double AnchorWidth { get; set; }
  28. public double DetailHeight { get; set; }
  29. }
  30. public delegate void DynamicSplitPanelChanged(object sender, DynamicSplitPanelSettings e);
  31. public class DynamicSplitPanel : Control
  32. {
  33. public static readonly DependencyProperty AllowableViewsProperty = DependencyProperty.Register(
  34. nameof(AllowableViews),
  35. typeof(DynamicSplitPanelView),
  36. typeof(DynamicSplitPanel),
  37. new UIPropertyMetadata( DynamicSplitPanelView.Master | DynamicSplitPanelView.Combined | DynamicSplitPanelView.Detail ));
  38. public static readonly DependencyProperty ViewProperty = DependencyProperty.Register("View", typeof(DynamicSplitPanelView),
  39. typeof(DynamicSplitPanel), new UIPropertyMetadata(DynamicSplitPanelView.Detail));
  40. public static readonly DependencyProperty AnchorProperty = DependencyProperty.Register(nameof(Anchor), typeof(DynamicSplitPanelAnchor),
  41. typeof(DynamicSplitPanel), new UIPropertyMetadata(DynamicSplitPanelAnchor.Master));
  42. public static readonly DependencyProperty AnchorWidthProperty =
  43. DependencyProperty.Register(nameof(AnchorWidth), typeof(double), typeof(DynamicSplitPanel), new UIPropertyMetadata((double)300F));
  44. public static readonly DependencyProperty MasterCaptionProperty =
  45. DependencyProperty.Register("MasterCaption", typeof(string), typeof(DynamicSplitPanel), new UIPropertyMetadata(null));
  46. public static readonly DependencyProperty DetailCaptionProperty =
  47. DependencyProperty.Register("DetailCaption", typeof(string), typeof(DynamicSplitPanel), new UIPropertyMetadata(null));
  48. public static readonly DependencyProperty HeaderProperty =
  49. DependencyProperty.Register("Header", typeof(FrameworkElement), typeof(DynamicSplitPanel), new UIPropertyMetadata(null));
  50. public static readonly DependencyProperty MasterProperty =
  51. DependencyProperty.Register(nameof(Master), typeof(FrameworkElement), typeof(DynamicSplitPanel), new UIPropertyMetadata(null));
  52. public static readonly DependencyProperty DetailHeaderProperty =
  53. DependencyProperty.Register("DetailHeader", typeof(FrameworkElement), typeof(DynamicSplitPanel), new UIPropertyMetadata(null));
  54. public static readonly DependencyProperty DetailProperty =
  55. DependencyProperty.Register("Detail", typeof(FrameworkElement), typeof(DynamicSplitPanel), new UIPropertyMetadata(null));
  56. public static readonly DependencyProperty DetailHeightProperty =
  57. DependencyProperty.Register("DetailHeight", typeof(double), typeof(DynamicSplitPanel), new UIPropertyMetadata((double)200F));
  58. public static readonly DependencyProperty SecondaryDetailProperty = DependencyProperty.Register("SecondaryDetail", typeof(FrameworkElement),
  59. typeof(DynamicSplitPanel), new UIPropertyMetadata(null));
  60. private Button CombinedLeft;
  61. private Button CombinedRight;
  62. private Grid DetailGrid;
  63. private Label DetailsHeader;
  64. private Button DetailsOnly;
  65. private SfGridSplitter DetailSplitter;
  66. private Grid Grid;
  67. private Label MasterHeader;
  68. private Button MasterOnly;
  69. private SfGridSplitter Splitter;
  70. static DynamicSplitPanel()
  71. {
  72. DefaultStyleKeyProperty.OverrideMetadata(typeof(DynamicSplitPanel), new FrameworkPropertyMetadata(typeof(DynamicSplitPanel)));
  73. }
  74. public DynamicSplitPanelView AllowableViews
  75. {
  76. get => (DynamicSplitPanelView)GetValue(AllowableViewsProperty);
  77. set
  78. {
  79. SetValue(AllowableViewsProperty, value);
  80. ConfigureScreen();
  81. }
  82. }
  83. private bool IsViewAllowed(params DynamicSplitPanelView[] views)
  84. {
  85. foreach (var view in views)
  86. {
  87. if ((AllowableViews & view) == view)
  88. return true;
  89. }
  90. return false;
  91. }
  92. public DynamicSplitPanelView View
  93. {
  94. get => (DynamicSplitPanelView)GetValue(ViewProperty);
  95. set
  96. {
  97. SetValue(ViewProperty, value);
  98. ConfigureScreen();
  99. }
  100. }
  101. public DynamicSplitPanelAnchor Anchor
  102. {
  103. get => (DynamicSplitPanelAnchor)GetValue(AnchorProperty);
  104. set
  105. {
  106. SetValue(AnchorProperty, value);
  107. ConfigureScreen();
  108. }
  109. }
  110. public double AnchorWidth
  111. {
  112. get => (double)GetValue(AnchorWidthProperty);
  113. set
  114. {
  115. SetValue(AnchorWidthProperty, value);
  116. ConfigureScreen();
  117. }
  118. }
  119. public string MasterCaption
  120. {
  121. get => (string)GetValue(MasterCaptionProperty);
  122. set => SetValue(MasterCaptionProperty, value);
  123. //MasterHeader.Content = value;
  124. }
  125. public string DetailCaption
  126. {
  127. get => (string)GetValue(DetailCaptionProperty);
  128. set => SetValue(DetailCaptionProperty, value);
  129. //DetailHeader.Content = value;
  130. }
  131. public FrameworkElement Header
  132. {
  133. get => (FrameworkElement)GetValue(HeaderProperty);
  134. set => SetValue(HeaderProperty, value);
  135. }
  136. public FrameworkElement Master
  137. {
  138. get => (FrameworkElement)GetValue(MasterProperty);
  139. set => SetValue(MasterProperty, value);
  140. }
  141. public FrameworkElement DetailHeader
  142. {
  143. get => (FrameworkElement)GetValue(DetailHeaderProperty);
  144. set => SetValue(DetailHeaderProperty, value);
  145. }
  146. public FrameworkElement Detail
  147. {
  148. get => (FrameworkElement)GetValue(DetailProperty);
  149. set => SetValue(DetailProperty, value);
  150. }
  151. public double DetailHeight
  152. {
  153. get => (double)GetValue(DetailHeightProperty);
  154. set
  155. {
  156. SetValue(DetailHeightProperty, value);
  157. ConfigureScreen();
  158. }
  159. }
  160. public FrameworkElement SecondaryDetail
  161. {
  162. get => (FrameworkElement)GetValue(SecondaryDetailProperty);
  163. set => SetValue(SecondaryDetailProperty, value);
  164. }
  165. public event DynamicSplitPanelChanged OnChanged;
  166. private void RightClick(object sender, RoutedEventArgs e)
  167. {
  168. if (View == DynamicSplitPanelView.Detail)
  169. View = DynamicSplitPanelView.Combined;
  170. else if (View == DynamicSplitPanelView.Combined)
  171. View = DynamicSplitPanelView.Master;
  172. Changed();
  173. }
  174. private void LeftClick(object sender, RoutedEventArgs e)
  175. {
  176. if (View == DynamicSplitPanelView.Master)
  177. View = DynamicSplitPanelView.Combined;
  178. else if (View == DynamicSplitPanelView.Combined)
  179. View = DynamicSplitPanelView.Detail;
  180. Changed();
  181. }
  182. public bool IsMasterVisible() => View == DynamicSplitPanelView.Master || View == DynamicSplitPanelView.Combined;
  183. public bool IsDetailVisible() => View == DynamicSplitPanelView.Detail || View == DynamicSplitPanelView.Combined;
  184. private void ConfigureScreen()
  185. {
  186. CheckParts();
  187. try
  188. {
  189. if (MasterHeader != null)
  190. MasterHeader.Content = MasterCaption;
  191. if (DetailsHeader != null)
  192. DetailsHeader.Content = DetailCaption;
  193. if (CombinedLeft != null)
  194. CombinedLeft.Visibility = View == DynamicSplitPanelView.Combined && AllowableViews.HasFlag(DynamicSplitPanelView.Master) ? Visibility.Visible : Visibility.Collapsed;
  195. if (CombinedRight != null)
  196. CombinedRight.Visibility = View == DynamicSplitPanelView.Combined && AllowableViews.HasFlag(DynamicSplitPanelView.Detail) ? Visibility.Visible : Visibility.Collapsed;
  197. if (Grid != null)
  198. {
  199. Grid.ColumnDefinitions[0].Width = (View == DynamicSplitPanelView.Detail)
  200. && (Master != null)
  201. && (IsViewAllowed(DynamicSplitPanelView.Combined,DynamicSplitPanelView.Master))
  202. ? new GridLength(1.0F, GridUnitType.Auto)
  203. : new GridLength(0.0F, GridUnitType.Pixel);
  204. Grid.ColumnDefinitions[1].Width = Master == null
  205. ? new GridLength(0.0F, GridUnitType.Pixel)
  206. : View switch {
  207. DynamicSplitPanelView.Master => new GridLength(1.0F, GridUnitType.Star),
  208. DynamicSplitPanelView.Detail => new GridLength(0.0F, GridUnitType.Pixel),
  209. _ => Anchor == DynamicSplitPanelAnchor.Master
  210. ? new GridLength(AnchorWidth, GridUnitType.Pixel)
  211. : new GridLength(1.0F, GridUnitType.Star)
  212. };
  213. Grid.ColumnDefinitions[2].Width = (View == DynamicSplitPanelView.Combined)
  214. && (Master != null)
  215. ? new GridLength(1.0F, GridUnitType.Auto)
  216. : new GridLength(0.0F, GridUnitType.Pixel);
  217. Grid.ColumnDefinitions[3].Width = View switch
  218. {
  219. DynamicSplitPanelView.Master => new GridLength(0.0F, GridUnitType.Pixel),
  220. DynamicSplitPanelView.Detail => new GridLength(1.0F, GridUnitType.Star),
  221. _ => Anchor == DynamicSplitPanelAnchor.Detail
  222. ? new GridLength(AnchorWidth, GridUnitType.Pixel)
  223. : new GridLength(1.0F, GridUnitType.Star)
  224. };
  225. Grid.ColumnDefinitions[4].Width = (View == DynamicSplitPanelView.Master)
  226. && (IsViewAllowed(DynamicSplitPanelView.Combined,DynamicSplitPanelView.Detail))
  227. ? new GridLength(1.0F, GridUnitType.Auto)
  228. : new GridLength(0.0F, GridUnitType.Pixel);
  229. Grid.RowDefinitions[0].Height =
  230. Header == null ? new GridLength(0.0F, GridUnitType.Pixel) : new GridLength(1.0F, GridUnitType.Auto);
  231. }
  232. if (DetailGrid != null)
  233. {
  234. DetailGrid.SetValue(Grid.RowProperty, DetailHeader == null ? 0 : 1);
  235. DetailGrid.SetValue(Grid.RowSpanProperty, DetailHeader == null ? 2 : 1);
  236. DetailGrid.RowDefinitions[0].Height = SecondaryDetail == null
  237. ? new GridLength(1.0F, GridUnitType.Star)
  238. : new GridLength(DetailHeight, GridUnitType.Pixel);
  239. DetailGrid.RowDefinitions[1].Height =
  240. SecondaryDetail == null ? new GridLength(0.0F, GridUnitType.Pixel) : new GridLength(1.0F, GridUnitType.Auto);
  241. DetailGrid.RowDefinitions[2].Height =
  242. SecondaryDetail == null ? new GridLength(0.0F, GridUnitType.Pixel) : new GridLength(1.0F, GridUnitType.Star);
  243. DetailSplitter.IsEnabled = SecondaryDetail != null;
  244. }
  245. if (CombinedRight != null)
  246. {
  247. CombinedRight.Visibility = (View == DynamicSplitPanelView.Combined) && IsViewAllowed(DynamicSplitPanelView.Master)
  248. ? Visibility.Visible
  249. : Visibility.Collapsed;
  250. }
  251. if (CombinedLeft != null)
  252. {
  253. CombinedLeft.Visibility = (View == DynamicSplitPanelView.Combined) && IsViewAllowed(DynamicSplitPanelView.Detail)
  254. ? Visibility.Visible
  255. : Visibility.Collapsed;
  256. }
  257. }
  258. catch (Exception e)
  259. {
  260. Logger.Send(LogType.Error, "", string.Format("*** Unknown Error: {0}\n{1}", e.Message, e.StackTrace));
  261. }
  262. }
  263. private void Splitter_PreviewMouseUp(object sender, MouseButtonEventArgs e)
  264. {
  265. if (Anchor == DynamicSplitPanelAnchor.Master)
  266. SetValue(AnchorWidthProperty, Grid.ColumnDefinitions[1].ActualWidth);
  267. else
  268. SetValue(AnchorWidthProperty, Grid.ColumnDefinitions[3].ActualWidth);
  269. SetValue(DetailHeightProperty, SecondaryDetail == null ? 0.0F : DetailGrid.RowDefinitions[0].ActualHeight);
  270. Changed();
  271. }
  272. private void Changed()
  273. {
  274. OnChanged?.Invoke(this, new DynamicSplitPanelSettings { View = View, AnchorWidth = AnchorWidth, DetailHeight = DetailHeight });
  275. }
  276. public override void OnApplyTemplate()
  277. {
  278. base.OnApplyTemplate();
  279. CheckParts();
  280. ConfigureScreen();
  281. }
  282. private void CheckParts()
  283. {
  284. try
  285. {
  286. if (Grid == null)
  287. Grid = GetTemplateChild("PART_Grid") as Grid;
  288. if (DetailGrid == null)
  289. DetailGrid = GetTemplateChild("PART_DetailGrid") as Grid;
  290. if (MasterHeader == null)
  291. MasterHeader = GetTemplateChild("PART_MasterHeader") as Label;
  292. if (DetailsHeader == null)
  293. DetailsHeader = GetTemplateChild("PART_DetailHeader") as Label;
  294. if (Splitter == null)
  295. {
  296. Splitter = GetTemplateChild("PART_Splitter") as SfGridSplitter;
  297. if (Splitter != null)
  298. {
  299. Splitter.PreviewMouseUp += Splitter_PreviewMouseUp;
  300. Splitter.TargetUpdated += Splitter_TargetUpdated;
  301. }
  302. }
  303. if (DetailSplitter == null)
  304. {
  305. DetailSplitter = GetTemplateChild("PART_DetailSplitter") as SfGridSplitter;
  306. if (DetailSplitter != null)
  307. DetailSplitter.PreviewMouseUp += Splitter_PreviewMouseUp;
  308. }
  309. if (DetailsOnly == null)
  310. {
  311. DetailsOnly = GetTemplateChild("PART_DetailsOnly") as Button;
  312. if (DetailsOnly != null)
  313. DetailsOnly.Click += RightClick;
  314. }
  315. if (CombinedRight == null)
  316. {
  317. CombinedRight = GetTemplateChild("PART_CombinedRight") as Button;
  318. if (CombinedRight != null)
  319. CombinedRight.Click += RightClick;
  320. }
  321. if (CombinedLeft == null)
  322. {
  323. CombinedLeft = GetTemplateChild("PART_CombinedLeft") as Button;
  324. if (CombinedLeft != null)
  325. CombinedLeft.Click += LeftClick;
  326. }
  327. if (MasterOnly == null)
  328. {
  329. MasterOnly = GetTemplateChild("PART_MasterOnly") as Button;
  330. if (MasterOnly != null)
  331. MasterOnly.Click += LeftClick;
  332. }
  333. }
  334. catch (Exception e)
  335. {
  336. Logger.Send(LogType.Error, "", string.Format("*** Unknown Error: {0}\n{1}", e.Message, e.StackTrace));
  337. }
  338. }
  339. private void Splitter_TargetUpdated(object sender, DataTransferEventArgs e)
  340. {
  341. //
  342. }
  343. }