DynamicSplitPanel.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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(nameof(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. public bool IsMasterVisible() => View == DynamicSplitPanelView.Master || View == DynamicSplitPanelView.Combined;
  184. public bool IsDetailVisible() => View == DynamicSplitPanelView.Detail || View == DynamicSplitPanelView.Combined;
  185. private void ConfigureScreen()
  186. {
  187. CheckParts();
  188. try
  189. {
  190. if (MasterHeader != null)
  191. MasterHeader.Content = MasterCaption;
  192. if (DetailsHeader != null)
  193. DetailsHeader.Content = DetailCaption;
  194. if (CombinedLeft != null)
  195. CombinedLeft.Visibility = View == DynamicSplitPanelView.Combined && AllowableViews.HasFlag(DynamicSplitPanelView.Master) ? Visibility.Visible : Visibility.Collapsed;
  196. if (CombinedRight != null)
  197. CombinedRight.Visibility = View == DynamicSplitPanelView.Combined && AllowableViews.HasFlag(DynamicSplitPanelView.Detail) ? Visibility.Visible : Visibility.Collapsed;
  198. if (Grid != null)
  199. {
  200. Grid.ColumnDefinitions[0].Width = (View == DynamicSplitPanelView.Detail)
  201. && (Master != null)
  202. && (IsViewAllowed(DynamicSplitPanelView.Combined,DynamicSplitPanelView.Master))
  203. ? new GridLength(1.0F, GridUnitType.Auto)
  204. : new GridLength(0.0F, GridUnitType.Pixel);
  205. Grid.ColumnDefinitions[1].Width = Master == null
  206. ? new GridLength(0.0F, GridUnitType.Pixel)
  207. : View switch {
  208. DynamicSplitPanelView.Master => new GridLength(1.0F, GridUnitType.Star),
  209. DynamicSplitPanelView.Detail => new GridLength(0.0F, GridUnitType.Pixel),
  210. _ => Anchor == DynamicSplitPanelAnchor.Master
  211. ? new GridLength(AnchorWidth, GridUnitType.Pixel)
  212. : new GridLength(1.0F, GridUnitType.Star)
  213. };
  214. Grid.ColumnDefinitions[2].Width = (View == DynamicSplitPanelView.Combined)
  215. && (Master != null)
  216. ? new GridLength(1.0F, GridUnitType.Auto)
  217. : new GridLength(0.0F, GridUnitType.Pixel);
  218. Grid.ColumnDefinitions[3].Width = View switch
  219. {
  220. DynamicSplitPanelView.Master => new GridLength(0.0F, GridUnitType.Pixel),
  221. DynamicSplitPanelView.Detail => new GridLength(1.0F, GridUnitType.Star),
  222. _ => Anchor == DynamicSplitPanelAnchor.Detail
  223. ? new GridLength(AnchorWidth, GridUnitType.Pixel)
  224. : new GridLength(1.0F, GridUnitType.Star)
  225. };
  226. Grid.ColumnDefinitions[4].Width = (View == DynamicSplitPanelView.Master)
  227. && (IsViewAllowed(DynamicSplitPanelView.Combined,DynamicSplitPanelView.Detail))
  228. ? new GridLength(1.0F, GridUnitType.Auto)
  229. : new GridLength(0.0F, GridUnitType.Pixel);
  230. Grid.RowDefinitions[0].Height =
  231. Header == null ? new GridLength(0.0F, GridUnitType.Pixel) : new GridLength(1.0F, GridUnitType.Auto);
  232. }
  233. if (DetailGrid != null)
  234. {
  235. DetailGrid.SetValue(Grid.RowProperty, DetailHeader == null ? 0 : 1);
  236. DetailGrid.SetValue(Grid.RowSpanProperty, DetailHeader == null ? 2 : 1);
  237. DetailGrid.RowDefinitions[0].Height = SecondaryDetail == null
  238. ? new GridLength(1.0F, GridUnitType.Star)
  239. : new GridLength(DetailHeight, GridUnitType.Pixel);
  240. DetailGrid.RowDefinitions[1].Height =
  241. SecondaryDetail == null ? new GridLength(0.0F, GridUnitType.Pixel) : new GridLength(1.0F, GridUnitType.Auto);
  242. DetailGrid.RowDefinitions[2].Height =
  243. SecondaryDetail == null ? new GridLength(0.0F, GridUnitType.Pixel) : new GridLength(1.0F, GridUnitType.Star);
  244. DetailSplitter.IsEnabled = SecondaryDetail != null;
  245. }
  246. if (CombinedRight != null)
  247. {
  248. CombinedRight.Visibility = (View == DynamicSplitPanelView.Combined) && IsViewAllowed(DynamicSplitPanelView.Master)
  249. ? Visibility.Visible
  250. : Visibility.Collapsed;
  251. }
  252. if (CombinedLeft != null)
  253. {
  254. CombinedLeft.Visibility = (View == DynamicSplitPanelView.Combined) && IsViewAllowed(DynamicSplitPanelView.Detail)
  255. ? Visibility.Visible
  256. : Visibility.Collapsed;
  257. }
  258. }
  259. catch (Exception e)
  260. {
  261. Logger.Send(LogType.Error, "", string.Format("*** Unknown Error: {0}\n{1}", e.Message, e.StackTrace));
  262. }
  263. }
  264. private void Splitter_PreviewMouseUp(object sender, MouseButtonEventArgs e)
  265. {
  266. if (Anchor == DynamicSplitPanelAnchor.Master)
  267. SetValue(AnchorWidthProperty, Grid.ColumnDefinitions[1].ActualWidth);
  268. else
  269. SetValue(AnchorWidthProperty, Grid.ColumnDefinitions[3].ActualWidth);
  270. SetValue(DetailHeightProperty, SecondaryDetail == null ? 0.0F : DetailGrid.RowDefinitions[0].ActualHeight);
  271. Changed();
  272. }
  273. private void Changed()
  274. {
  275. OnChanged?.Invoke(this, new DynamicSplitPanelSettings { View = View, AnchorWidth = AnchorWidth, DetailHeight = DetailHeight });
  276. }
  277. public override void OnApplyTemplate()
  278. {
  279. base.OnApplyTemplate();
  280. CheckParts();
  281. ConfigureScreen();
  282. }
  283. private void CheckParts()
  284. {
  285. try
  286. {
  287. if (Grid == null)
  288. Grid = GetTemplateChild("PART_Grid") as Grid;
  289. if (DetailGrid == null)
  290. DetailGrid = GetTemplateChild("PART_DetailGrid") as Grid;
  291. if (MasterHeader == null)
  292. MasterHeader = GetTemplateChild("PART_MasterHeader") as Label;
  293. if (DetailsHeader == null)
  294. DetailsHeader = GetTemplateChild("PART_DetailHeader") as Label;
  295. if (Splitter == null)
  296. {
  297. Splitter = GetTemplateChild("PART_Splitter") as SfGridSplitter;
  298. if (Splitter != null)
  299. {
  300. Splitter.PreviewMouseUp += Splitter_PreviewMouseUp;
  301. Splitter.TargetUpdated += Splitter_TargetUpdated;
  302. }
  303. }
  304. if (DetailSplitter == null)
  305. {
  306. DetailSplitter = GetTemplateChild("PART_DetailSplitter") as SfGridSplitter;
  307. if (DetailSplitter != null)
  308. DetailSplitter.PreviewMouseUp += Splitter_PreviewMouseUp;
  309. }
  310. if (DetailsOnly == null)
  311. {
  312. DetailsOnly = GetTemplateChild("PART_DetailsOnly") as Button;
  313. if (DetailsOnly != null)
  314. DetailsOnly.Click += RightClick;
  315. }
  316. if (CombinedRight == null)
  317. {
  318. CombinedRight = GetTemplateChild("PART_CombinedRight") as Button;
  319. if (CombinedRight != null)
  320. CombinedRight.Click += RightClick;
  321. }
  322. if (CombinedLeft == null)
  323. {
  324. CombinedLeft = GetTemplateChild("PART_CombinedLeft") as Button;
  325. if (CombinedLeft != null)
  326. CombinedLeft.Click += LeftClick;
  327. }
  328. if (MasterOnly == null)
  329. {
  330. MasterOnly = GetTemplateChild("PART_MasterOnly") as Button;
  331. if (MasterOnly != null)
  332. MasterOnly.Click += LeftClick;
  333. }
  334. }
  335. catch (Exception e)
  336. {
  337. Logger.Send(LogType.Error, "", string.Format("*** Unknown Error: {0}\n{1}", e.Message, e.StackTrace));
  338. }
  339. }
  340. private void Splitter_TargetUpdated(object sender, DataTransferEventArgs e)
  341. {
  342. //
  343. }
  344. }
  345. }