DynamicSplitPanel.cs 16 KB

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