DynamicMenuBar.cs 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126
  1. using System.Text;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Reflection;
  7. using System.Runtime.CompilerServices;
  8. using Terminal.Gui;
  9. namespace UICatalog.Scenarios {
  10. [ScenarioMetadata (Name: "Dynamic MenuBar", Description: "Demonstrates how to change a MenuBar dynamically.")]
  11. [ScenarioCategory ("Top Level Windows")]
  12. [ScenarioCategory ("Menus")]
  13. public class DynamicMenuBar : Scenario {
  14. public override void Init ()
  15. {
  16. Application.Init ();
  17. Application.Top.Add (new DynamicMenuBarSample () { Title = $"{Application.QuitKey} to Quit - Scenario: {GetName ()}" });
  18. }
  19. public class DynamicMenuItemList {
  20. public string Title { get; set; }
  21. public MenuItem MenuItem { get; set; }
  22. public DynamicMenuItemList () { }
  23. public DynamicMenuItemList (string title, MenuItem menuItem)
  24. {
  25. Title = title;
  26. MenuItem = menuItem;
  27. }
  28. public override string ToString () => $"{Title}, {MenuItem}";
  29. }
  30. public class DynamicMenuItem {
  31. public string title = "_New";
  32. public string help = "";
  33. public string action = "";
  34. public bool isTopLevel;
  35. public bool hasSubMenu;
  36. public MenuItemCheckStyle checkStyle;
  37. public string shortcut;
  38. public bool allowNullChecked;
  39. public DynamicMenuItem () { }
  40. public DynamicMenuItem (string title, bool hasSubMenu = false)
  41. {
  42. this.title = title;
  43. this.hasSubMenu = hasSubMenu;
  44. }
  45. public DynamicMenuItem (string title, string help, string action, bool isTopLevel, bool hasSubMenu, MenuItemCheckStyle checkStyle = MenuItemCheckStyle.NoCheck, string shortcut = null, bool allowNullChecked = false)
  46. {
  47. this.title = title;
  48. this.help = help;
  49. this.action = action;
  50. this.isTopLevel = isTopLevel;
  51. this.hasSubMenu = hasSubMenu;
  52. this.checkStyle = checkStyle;
  53. this.shortcut = shortcut;
  54. this.allowNullChecked = allowNullChecked;
  55. }
  56. }
  57. public class DynamicMenuBarSample : Window {
  58. MenuBar _menuBar;
  59. MenuItem _currentMenuBarItem;
  60. int _currentSelectedMenuBar;
  61. MenuItem _currentEditMenuBarItem;
  62. ListView _lstMenus;
  63. public DynamicMenuItemModel DataContext { get; set; }
  64. public DynamicMenuBarSample () : base ()
  65. {
  66. DataContext = new DynamicMenuItemModel ();
  67. var _frmDelimiter = new FrameView ("Shortcut Delimiter:") {
  68. X = Pos.Center (),
  69. Y = 3,
  70. Width = 25,
  71. Height = 4
  72. };
  73. var _txtDelimiter = new TextField (MenuBar.ShortcutDelimiter.ToString()) {
  74. X = Pos.Center (),
  75. Width = 2,
  76. };
  77. _txtDelimiter.TextChanged += (s, _) => MenuBar.ShortcutDelimiter = _txtDelimiter.Text.ToRunes()[0];
  78. _frmDelimiter.Add (_txtDelimiter);
  79. Add (_frmDelimiter);
  80. var _frmMenu = new FrameView ("Menus:") {
  81. Y = 7,
  82. Width = Dim.Percent (50),
  83. Height = Dim.Fill ()
  84. };
  85. var _btnAddMenuBar = new Button ("Add a MenuBar") {
  86. Y = 1,
  87. };
  88. _frmMenu.Add (_btnAddMenuBar);
  89. var _btnMenuBarUp = new Button ("^") {
  90. X = Pos.Center ()
  91. };
  92. _frmMenu.Add (_btnMenuBarUp);
  93. var _btnMenuBarDown = new Button ("v") {
  94. X = Pos.Center (),
  95. Y = Pos.Bottom (_btnMenuBarUp)
  96. };
  97. _frmMenu.Add (_btnMenuBarDown);
  98. var _btnRemoveMenuBar = new Button ("Remove a MenuBar") {
  99. Y = 1
  100. };
  101. _btnRemoveMenuBar.X = Pos.AnchorEnd () - (Pos.Right (_btnRemoveMenuBar) - Pos.Left (_btnRemoveMenuBar));
  102. _frmMenu.Add (_btnRemoveMenuBar);
  103. var _btnPrevious = new Button ("<") {
  104. X = Pos.Left (_btnAddMenuBar),
  105. Y = Pos.Top (_btnAddMenuBar) + 2
  106. };
  107. _frmMenu.Add (_btnPrevious);
  108. var _btnAdd = new Button (" Add ") {
  109. Y = Pos.Top (_btnPrevious) + 2,
  110. };
  111. _btnAdd.X = Pos.AnchorEnd () - (Pos.Right (_btnAdd) - Pos.Left (_btnAdd));
  112. _frmMenu.Add (_btnAdd);
  113. var _btnNext = new Button (">") {
  114. X = Pos.X (_btnAdd),
  115. Y = Pos.Top (_btnPrevious),
  116. };
  117. _frmMenu.Add (_btnNext);
  118. var _lblMenuBar = new Label () {
  119. ColorScheme = Colors.ColorSchemes ["Dialog"],
  120. TextAlignment = TextAlignment.Centered,
  121. X = Pos.Right (_btnPrevious) + 1,
  122. Y = Pos.Top (_btnPrevious),
  123. Width = Dim.Fill () - Dim.Function (() => _btnAdd.Frame.Width + 1),
  124. Height = 1
  125. };
  126. _frmMenu.Add (_lblMenuBar);
  127. _lblMenuBar.WantMousePositionReports = true;
  128. _lblMenuBar.CanFocus = true;
  129. var _lblParent = new Label () {
  130. TextAlignment = TextAlignment.Centered,
  131. X = Pos.Right (_btnPrevious) + 1,
  132. Y = Pos.Top (_btnPrevious) + 1,
  133. Width = Dim.Fill () - Dim.Width (_btnAdd) - 1
  134. };
  135. _frmMenu.Add (_lblParent);
  136. var _btnPreviowsParent = new Button ("..") {
  137. X = Pos.Left (_btnAddMenuBar),
  138. Y = Pos.Top (_btnPrevious) + 1
  139. };
  140. _frmMenu.Add (_btnPreviowsParent);
  141. _lstMenus = new ListView (new List<DynamicMenuItemList> ()) {
  142. ColorScheme = Colors.ColorSchemes ["Dialog"],
  143. X = Pos.Right (_btnPrevious) + 1,
  144. Y = Pos.Top (_btnPrevious) + 2,
  145. Width = _lblMenuBar.Width,
  146. Height = Dim.Fill (),
  147. };
  148. _frmMenu.Add (_lstMenus);
  149. _lblMenuBar.TabIndex = _btnPrevious.TabIndex + 1;
  150. _lstMenus.TabIndex = _lblMenuBar.TabIndex + 1;
  151. _btnNext.TabIndex = _lstMenus.TabIndex + 1;
  152. _btnAdd.TabIndex = _btnNext.TabIndex + 1;
  153. var _btnRemove = new Button ("Remove") {
  154. X = Pos.Left (_btnAdd),
  155. Y = Pos.Top (_btnAdd) + 1
  156. };
  157. _frmMenu.Add (_btnRemove);
  158. var _btnUp = new Button ("^") {
  159. X = Pos.Right (_lstMenus) + 2,
  160. Y = Pos.Top (_btnRemove) + 2
  161. };
  162. _frmMenu.Add (_btnUp);
  163. var _btnDown = new Button ("v") {
  164. X = Pos.Right (_lstMenus) + 2,
  165. Y = Pos.Top (_btnUp) + 1
  166. };
  167. _frmMenu.Add (_btnDown);
  168. Add (_frmMenu);
  169. var _frmMenuDetails = new DynamicMenuBarDetails ("Menu Details:") {
  170. X = Pos.Right (_frmMenu),
  171. Y = Pos.Top (_frmMenu),
  172. Width = Dim.Fill (),
  173. Height = Dim.Fill (2)
  174. };
  175. Add (_frmMenuDetails);
  176. _btnMenuBarUp.Clicked += (s, e) => {
  177. var i = _currentSelectedMenuBar;
  178. var menuItem = _menuBar != null && _menuBar.Menus.Length > 0 ? _menuBar.Menus [i] : null;
  179. if (menuItem != null) {
  180. var menus = _menuBar.Menus;
  181. if (i > 0) {
  182. menus [i] = menus [i - 1];
  183. menus [i - 1] = menuItem;
  184. _currentSelectedMenuBar = i - 1;
  185. _menuBar.SetNeedsDisplay ();
  186. }
  187. }
  188. };
  189. _btnMenuBarDown.Clicked += (s, e) => {
  190. var i = _currentSelectedMenuBar;
  191. var menuItem = _menuBar != null && _menuBar.Menus.Length > 0 ? _menuBar.Menus [i] : null;
  192. if (menuItem != null) {
  193. var menus = _menuBar.Menus;
  194. if (i < menus.Length - 1) {
  195. menus [i] = menus [i + 1];
  196. menus [i + 1] = menuItem;
  197. _currentSelectedMenuBar = i + 1;
  198. _menuBar.SetNeedsDisplay ();
  199. }
  200. }
  201. };
  202. _btnUp.Clicked += (s, e) => {
  203. var i = _lstMenus.SelectedItem;
  204. var menuItem = DataContext.Menus.Count > 0 ? DataContext.Menus [i].MenuItem : null;
  205. if (menuItem != null) {
  206. var childrens = ((MenuBarItem)_currentMenuBarItem).Children;
  207. if (i > 0) {
  208. childrens [i] = childrens [i - 1];
  209. childrens [i - 1] = menuItem;
  210. DataContext.Menus [i] = DataContext.Menus [i - 1];
  211. DataContext.Menus [i - 1] = new DynamicMenuItemList (menuItem.Title, menuItem);
  212. _lstMenus.SelectedItem = i - 1;
  213. }
  214. }
  215. };
  216. _btnDown.Clicked += (s, e) => {
  217. var i = _lstMenus.SelectedItem;
  218. var menuItem = DataContext.Menus.Count > 0 ? DataContext.Menus [i].MenuItem : null;
  219. if (menuItem != null) {
  220. var childrens = ((MenuBarItem)_currentMenuBarItem).Children;
  221. if (i < childrens.Length - 1) {
  222. childrens [i] = childrens [i + 1];
  223. childrens [i + 1] = menuItem;
  224. DataContext.Menus [i] = DataContext.Menus [i + 1];
  225. DataContext.Menus [i + 1] = new DynamicMenuItemList (menuItem.Title, menuItem);
  226. _lstMenus.SelectedItem = i + 1;
  227. }
  228. }
  229. };
  230. _btnPreviowsParent.Clicked += (s, e) => {
  231. if (_currentMenuBarItem != null && _currentMenuBarItem.Parent != null) {
  232. var mi = _currentMenuBarItem;
  233. _currentMenuBarItem = _currentMenuBarItem.Parent as MenuBarItem;
  234. SetListViewSource (_currentMenuBarItem, true);
  235. var i = ((MenuBarItem)_currentMenuBarItem).GetChildrenIndex (mi);
  236. if (i > -1) {
  237. _lstMenus.SelectedItem = i;
  238. }
  239. if (_currentMenuBarItem.Parent != null) {
  240. DataContext.Parent = _currentMenuBarItem.Title;
  241. } else {
  242. DataContext.Parent = string.Empty;
  243. }
  244. } else {
  245. DataContext.Parent = string.Empty;
  246. }
  247. };
  248. var _btnOk = new Button ("Ok") {
  249. X = Pos.Right (_frmMenu) + 20,
  250. Y = Pos.Bottom (_frmMenuDetails),
  251. };
  252. Add (_btnOk);
  253. var _btnCancel = new Button ("Cancel") {
  254. X = Pos.Right (_btnOk) + 3,
  255. Y = Pos.Top (_btnOk),
  256. };
  257. _btnCancel.Clicked += (s, e) => {
  258. SetFrameDetails (_currentEditMenuBarItem);
  259. };
  260. Add (_btnCancel);
  261. _lstMenus.SelectedItemChanged += (s, e) => {
  262. SetFrameDetails ();
  263. };
  264. _btnOk.Clicked += (s, e) => {
  265. if (string.IsNullOrEmpty (_frmMenuDetails._txtTitle.Text) && _currentEditMenuBarItem != null) {
  266. MessageBox.ErrorQuery ("Invalid title", "Must enter a valid title!.", "Ok");
  267. } else if (_currentEditMenuBarItem != null) {
  268. var menuItem = new DynamicMenuItem (_frmMenuDetails._txtTitle.Text, _frmMenuDetails._txtHelp.Text,
  269. _frmMenuDetails._txtAction.Text,
  270. _frmMenuDetails._ckbIsTopLevel != null ? (bool)_frmMenuDetails._ckbIsTopLevel.Checked : false,
  271. _frmMenuDetails._ckbSubMenu != null ? (bool)_frmMenuDetails._ckbSubMenu.Checked : false,
  272. _frmMenuDetails._rbChkStyle.SelectedItem == 0 ? MenuItemCheckStyle.NoCheck :
  273. _frmMenuDetails._rbChkStyle.SelectedItem == 1 ? MenuItemCheckStyle.Checked :
  274. MenuItemCheckStyle.Radio,
  275. _frmMenuDetails._txtShortcut.Text);
  276. UpdateMenuItem (_currentEditMenuBarItem, menuItem, _lstMenus.SelectedItem);
  277. }
  278. };
  279. _btnAdd.Clicked += (s, e) => {
  280. if (MenuBar == null) {
  281. MessageBox.ErrorQuery ("Menu Bar Error", "Must add a MenuBar first!", "Ok");
  282. _btnAddMenuBar.SetFocus ();
  283. return;
  284. }
  285. var frameDetails = new DynamicMenuBarDetails (null, _currentMenuBarItem != null);
  286. var item = frameDetails.EnterMenuItem ();
  287. if (item == null) {
  288. return;
  289. }
  290. if (!(_currentMenuBarItem is MenuBarItem)) {
  291. var parent = _currentMenuBarItem.Parent as MenuBarItem;
  292. var idx = parent.GetChildrenIndex (_currentMenuBarItem);
  293. _currentMenuBarItem = new MenuBarItem (_currentMenuBarItem.Title, new MenuItem [] { }, _currentMenuBarItem.Parent);
  294. _currentMenuBarItem.CheckType = item.checkStyle;
  295. parent.Children [idx] = _currentMenuBarItem;
  296. } else {
  297. MenuItem newMenu = CreateNewMenu (item, _currentMenuBarItem);
  298. var menuBarItem = _currentMenuBarItem as MenuBarItem;
  299. if (menuBarItem == null) {
  300. menuBarItem = new MenuBarItem (_currentMenuBarItem.Title, new MenuItem [] { newMenu }, _currentMenuBarItem.Parent);
  301. } else if (menuBarItem.Children == null) {
  302. menuBarItem.Children = new MenuItem [] { newMenu };
  303. } else {
  304. var childrens = menuBarItem.Children;
  305. Array.Resize (ref childrens, childrens.Length + 1);
  306. childrens [childrens.Length - 1] = newMenu;
  307. menuBarItem.Children = childrens;
  308. }
  309. DataContext.Menus.Add (new DynamicMenuItemList (newMenu.Title, newMenu));
  310. _lstMenus.MoveDown ();
  311. }
  312. };
  313. _btnRemove.Clicked += (s, e) => {
  314. var menuItem = DataContext.Menus.Count > 0 ? DataContext.Menus [_lstMenus.SelectedItem].MenuItem : null;
  315. if (menuItem != null) {
  316. var childrens = ((MenuBarItem)_currentMenuBarItem).Children;
  317. childrens [_lstMenus.SelectedItem] = null;
  318. int i = 0;
  319. foreach (var c in childrens) {
  320. if (c != null) {
  321. childrens [i] = c;
  322. i++;
  323. }
  324. }
  325. Array.Resize (ref childrens, childrens.Length - 1);
  326. if (childrens.Length == 0) {
  327. if (_currentMenuBarItem.Parent == null) {
  328. ((MenuBarItem)_currentMenuBarItem).Children = null;
  329. //_currentMenuBarItem.Action = _frmMenuDetails.CreateAction (_currentEditMenuBarItem, new DynamicMenuItem (_currentMenuBarItem.Title));
  330. } else {
  331. _currentMenuBarItem = new MenuItem (_currentMenuBarItem.Title, _currentMenuBarItem.Help, _frmMenuDetails.CreateAction (_currentEditMenuBarItem, new DynamicMenuItem (_currentEditMenuBarItem.Title)), null, _currentMenuBarItem.Parent);
  332. }
  333. } else {
  334. ((MenuBarItem)_currentMenuBarItem).Children = childrens;
  335. }
  336. DataContext.Menus.RemoveAt (_lstMenus.SelectedItem);
  337. if (_lstMenus.Source.Count > 0 && _lstMenus.SelectedItem > _lstMenus.Source.Count - 1) {
  338. _lstMenus.SelectedItem = _lstMenus.Source.Count - 1;
  339. }
  340. _lstMenus.SetNeedsDisplay ();
  341. SetFrameDetails ();
  342. }
  343. };
  344. _lstMenus.OpenSelectedItem += (s, e) => {
  345. _currentMenuBarItem = DataContext.Menus [e.Item].MenuItem;
  346. if (!(_currentMenuBarItem is MenuBarItem)) {
  347. MessageBox.ErrorQuery ("Menu Open Error", "Must allows sub menus first!", "Ok");
  348. return;
  349. }
  350. DataContext.Parent = _currentMenuBarItem.Title;
  351. DataContext.Menus = new List<DynamicMenuItemList> ();
  352. SetListViewSource (_currentMenuBarItem, true);
  353. var menuBarItem = DataContext.Menus.Count > 0 ? DataContext.Menus [0].MenuItem : null;
  354. SetFrameDetails (menuBarItem);
  355. };
  356. _lstMenus.Enter += (s, e) => {
  357. var menuBarItem = _lstMenus.SelectedItem > -1 && DataContext.Menus.Count > 0 ? DataContext.Menus [_lstMenus.SelectedItem].MenuItem : null;
  358. SetFrameDetails (menuBarItem);
  359. };
  360. _btnNext.Clicked += (s, e) => {
  361. if (_menuBar != null && _currentSelectedMenuBar + 1 < _menuBar.Menus.Length) {
  362. _currentSelectedMenuBar++;
  363. }
  364. SelectCurrentMenuBarItem ();
  365. };
  366. _btnPrevious.Clicked += (s, e) => {
  367. if (_currentSelectedMenuBar - 1 > -1) {
  368. _currentSelectedMenuBar--;
  369. }
  370. SelectCurrentMenuBarItem ();
  371. };
  372. _lblMenuBar.Enter += (s, e) => {
  373. if (_menuBar?.Menus != null) {
  374. _currentMenuBarItem = _menuBar.Menus [_currentSelectedMenuBar];
  375. SetFrameDetails (_menuBar.Menus [_currentSelectedMenuBar]);
  376. }
  377. };
  378. _btnAddMenuBar.Clicked += (s, e) => {
  379. var frameDetails = new DynamicMenuBarDetails (null, false);
  380. var item = frameDetails.EnterMenuItem ();
  381. if (item == null) {
  382. return;
  383. }
  384. if (MenuBar == null) {
  385. _menuBar = new MenuBar ();
  386. Add (_menuBar);
  387. }
  388. var newMenu = CreateNewMenu (item) as MenuBarItem;
  389. var menus = _menuBar.Menus;
  390. Array.Resize (ref menus, menus.Length + 1);
  391. menus [^1] = newMenu;
  392. _menuBar.Menus = menus;
  393. _currentMenuBarItem = newMenu;
  394. _currentMenuBarItem.CheckType = item.checkStyle;
  395. _currentSelectedMenuBar = menus.Length - 1;
  396. _menuBar.Menus [_currentSelectedMenuBar] = newMenu;
  397. _lblMenuBar.Text = newMenu.Title;
  398. SetListViewSource (_currentMenuBarItem, true);
  399. SetFrameDetails (_menuBar.Menus [_currentSelectedMenuBar]);
  400. _menuBar.SetNeedsDisplay ();
  401. };
  402. _btnRemoveMenuBar.Clicked += (s, e) => {
  403. if (_menuBar == null || _menuBar.Menus.Length == 0) {
  404. return;
  405. }
  406. if (_menuBar != null && _menuBar.Menus.Length > 0) {
  407. _menuBar.Menus [_currentSelectedMenuBar] = null;
  408. int i = 0;
  409. foreach (var m in _menuBar.Menus) {
  410. if (m != null) {
  411. _menuBar.Menus [i] = m;
  412. i++;
  413. }
  414. }
  415. var menus = _menuBar.Menus;
  416. Array.Resize (ref menus, menus.Length - 1);
  417. _menuBar.Menus = menus;
  418. if (_currentSelectedMenuBar - 1 >= 0 && _menuBar.Menus.Length > 0) {
  419. _currentSelectedMenuBar--;
  420. }
  421. _currentMenuBarItem = _menuBar.Menus?.Length > 0 ? _menuBar.Menus [_currentSelectedMenuBar] : null;
  422. }
  423. if (MenuBar != null && _currentMenuBarItem == null && _menuBar.Menus.Length == 0) {
  424. Remove (_menuBar);
  425. _menuBar = null;
  426. DataContext.Menus = new List<DynamicMenuItemList> ();
  427. _currentMenuBarItem = null;
  428. _currentSelectedMenuBar = -1;
  429. _lblMenuBar.Text = string.Empty;
  430. } else {
  431. _lblMenuBar.Text = _menuBar.Menus [_currentSelectedMenuBar].Title;
  432. }
  433. SetListViewSource (_currentMenuBarItem, true);
  434. SetFrameDetails (null);
  435. };
  436. SetFrameDetails ();
  437. var ustringConverter = new UStringValueConverter ();
  438. var listWrapperConverter = new ListWrapperConverter ();
  439. var lblMenuBar = new Binding (this, "MenuBar", _lblMenuBar, "Text", ustringConverter);
  440. var lblParent = new Binding (this, "Parent", _lblParent, "Text", ustringConverter);
  441. var lstMenus = new Binding (this, "Menus", _lstMenus, "Source", listWrapperConverter);
  442. void SetFrameDetails (MenuItem menuBarItem = null)
  443. {
  444. MenuItem menuItem;
  445. if (menuBarItem == null) {
  446. menuItem = _lstMenus.SelectedItem > -1 && DataContext.Menus.Count > 0 ? DataContext.Menus [_lstMenus.SelectedItem].MenuItem : null;
  447. } else {
  448. menuItem = menuBarItem;
  449. }
  450. _currentEditMenuBarItem = menuItem;
  451. _frmMenuDetails.EditMenuBarItem (menuItem);
  452. var f = _btnOk.Enabled == _frmMenuDetails.Enabled;
  453. if (!f) {
  454. _btnOk.Enabled = _frmMenuDetails.Enabled;
  455. _btnCancel.Enabled = _frmMenuDetails.Enabled;
  456. }
  457. }
  458. void SelectCurrentMenuBarItem ()
  459. {
  460. MenuBarItem menuBarItem = null;
  461. if (_menuBar?.Menus != null) {
  462. menuBarItem = _menuBar.Menus [_currentSelectedMenuBar];
  463. _lblMenuBar.Text = menuBarItem.Title;
  464. }
  465. SetFrameDetails (menuBarItem);
  466. _currentMenuBarItem = menuBarItem;
  467. DataContext.Menus = new List<DynamicMenuItemList> ();
  468. SetListViewSource (_currentMenuBarItem, true);
  469. _lblParent.Text = string.Empty;
  470. }
  471. void SetListViewSource (MenuItem _currentMenuBarItem, bool fill = false)
  472. {
  473. DataContext.Menus = new List<DynamicMenuItemList> ();
  474. var menuBarItem = _currentMenuBarItem as MenuBarItem;
  475. if (menuBarItem != null && menuBarItem?.Children == null) {
  476. return;
  477. }
  478. if (!fill) {
  479. return;
  480. }
  481. if (menuBarItem != null) {
  482. foreach (var child in menuBarItem?.Children) {
  483. var m = new DynamicMenuItemList (child.Title, child);
  484. DataContext.Menus.Add (m);
  485. }
  486. }
  487. }
  488. MenuItem CreateNewMenu (DynamicMenuItem item, MenuItem parent = null)
  489. {
  490. MenuItem newMenu;
  491. if (item.hasSubMenu) {
  492. newMenu = new MenuBarItem (item.title, new MenuItem [] { }, parent);
  493. } else if (parent != null) {
  494. newMenu = new MenuItem (item.title, item.help, null, null, parent);
  495. newMenu.CheckType = item.checkStyle;
  496. newMenu.Action = _frmMenuDetails.CreateAction (newMenu, item);
  497. newMenu.Shortcut = ShortcutHelper.GetShortcutFromTag (item.shortcut);
  498. newMenu.AllowNullChecked = item.allowNullChecked;
  499. } else if (item.isTopLevel) {
  500. newMenu = new MenuBarItem (item.title, item.help, null);
  501. newMenu.Action = _frmMenuDetails.CreateAction (newMenu, item);
  502. } else {
  503. newMenu = new MenuBarItem (item.title, item.help, null);
  504. ((MenuBarItem)newMenu).Children [0].Action = _frmMenuDetails.CreateAction (newMenu, item);
  505. ((MenuBarItem)newMenu).Children [0].Shortcut = ShortcutHelper.GetShortcutFromTag (item.shortcut);
  506. }
  507. return newMenu;
  508. }
  509. void UpdateMenuItem (MenuItem _currentEditMenuBarItem, DynamicMenuItem menuItem, int index)
  510. {
  511. _currentEditMenuBarItem.Title = menuItem.title;
  512. _currentEditMenuBarItem.Help = menuItem.help;
  513. _currentEditMenuBarItem.CheckType = menuItem.checkStyle;
  514. var parent = _currentEditMenuBarItem.Parent as MenuBarItem;
  515. if (parent != null && parent.Children.Length == 1 && _currentEditMenuBarItem.CheckType == MenuItemCheckStyle.Radio) {
  516. _currentEditMenuBarItem.Checked = true;
  517. }
  518. if (menuItem.isTopLevel && _currentEditMenuBarItem is MenuBarItem) {
  519. ((MenuBarItem)_currentEditMenuBarItem).Children = null;
  520. _currentEditMenuBarItem.Action = _frmMenuDetails.CreateAction (_currentEditMenuBarItem, menuItem);
  521. SetListViewSource (_currentEditMenuBarItem, true);
  522. } else if (menuItem.hasSubMenu) {
  523. _currentEditMenuBarItem.Action = null;
  524. if (_currentEditMenuBarItem is MenuBarItem && ((MenuBarItem)_currentEditMenuBarItem).Children == null) {
  525. ((MenuBarItem)_currentEditMenuBarItem).Children = new MenuItem [] { };
  526. } else if (_currentEditMenuBarItem.Parent != null) {
  527. _frmMenuDetails.UpdateParent (ref _currentEditMenuBarItem);
  528. } else {
  529. _currentEditMenuBarItem = new MenuBarItem (_currentEditMenuBarItem.Title, new MenuItem [] { }, _currentEditMenuBarItem.Parent);
  530. }
  531. SetListViewSource (_currentEditMenuBarItem, true);
  532. } else if (_currentEditMenuBarItem is MenuBarItem && _currentEditMenuBarItem.Parent != null) {
  533. _frmMenuDetails.UpdateParent (ref _currentEditMenuBarItem);
  534. _currentEditMenuBarItem = new MenuItem (menuItem.title, menuItem.help, _frmMenuDetails.CreateAction (_currentEditMenuBarItem, menuItem), null, _currentEditMenuBarItem.Parent);
  535. } else {
  536. if (_currentEditMenuBarItem is MenuBarItem) {
  537. ((MenuBarItem)_currentEditMenuBarItem).Children = null;
  538. DataContext.Menus = new List<DynamicMenuItemList> ();
  539. }
  540. _currentEditMenuBarItem.Action = _frmMenuDetails.CreateAction (_currentEditMenuBarItem, menuItem);
  541. _currentEditMenuBarItem.Shortcut = ShortcutHelper.GetShortcutFromTag (menuItem.shortcut);
  542. }
  543. if (_currentEditMenuBarItem.Parent == null) {
  544. DataContext.MenuBar = _currentEditMenuBarItem.Title;
  545. } else {
  546. if (DataContext.Menus.Count == 0) {
  547. DataContext.Menus.Add (new DynamicMenuItemList (_currentEditMenuBarItem.Title, _currentEditMenuBarItem));
  548. }
  549. DataContext.Menus [index] = new DynamicMenuItemList (_currentEditMenuBarItem.Title, _currentEditMenuBarItem);
  550. }
  551. _currentEditMenuBarItem.CheckType = menuItem.checkStyle;
  552. SetFrameDetails (_currentEditMenuBarItem);
  553. }
  554. //_frmMenuDetails.Initialized += (s, e) => _frmMenuDetails.Enabled = false;
  555. }
  556. }
  557. public class DynamicMenuBarDetails : FrameView {
  558. public MenuItem _menuItem;
  559. public TextField _txtTitle;
  560. public TextField _txtHelp;
  561. public TextView _txtAction;
  562. public CheckBox _ckbIsTopLevel;
  563. public CheckBox _ckbSubMenu;
  564. public CheckBox _ckbNullCheck;
  565. public RadioGroup _rbChkStyle;
  566. public TextField _txtShortcut;
  567. bool hasParent;
  568. public DynamicMenuBarDetails (MenuItem menuItem = null, bool hasParent = false) : this (menuItem == null ? "Adding New Menu." : "Editing Menu.")
  569. {
  570. _menuItem = menuItem;
  571. this.hasParent = hasParent;
  572. }
  573. public DynamicMenuBarDetails (string title) : base (title)
  574. {
  575. var _lblTitle = new Label ("Title:") {
  576. Y = 1
  577. };
  578. Add (_lblTitle);
  579. _txtTitle = new TextField () {
  580. X = Pos.Right (_lblTitle) + 2,
  581. Y = Pos.Top (_lblTitle),
  582. Width = Dim.Fill ()
  583. };
  584. Add (_txtTitle);
  585. var _lblHelp = new Label ("Help:") {
  586. X = Pos.Left (_lblTitle),
  587. Y = Pos.Bottom (_lblTitle) + 1
  588. };
  589. Add (_lblHelp);
  590. _txtHelp = new TextField () {
  591. X = Pos.Left (_txtTitle),
  592. Y = Pos.Top (_lblHelp),
  593. Width = Dim.Fill ()
  594. };
  595. Add (_txtHelp);
  596. var _lblAction = new Label ("Action:") {
  597. X = Pos.Left (_lblTitle),
  598. Y = Pos.Bottom (_lblHelp) + 1
  599. };
  600. Add (_lblAction);
  601. _txtAction = new TextView () {
  602. X = Pos.Left (_txtTitle),
  603. Y = Pos.Top (_lblAction),
  604. Width = Dim.Fill (),
  605. Height = 5
  606. };
  607. Add (_txtAction);
  608. _ckbIsTopLevel = new CheckBox ("IsTopLevel") {
  609. X = Pos.Left (_lblTitle),
  610. Y = Pos.Bottom (_lblAction) + 5
  611. };
  612. Add (_ckbIsTopLevel);
  613. _ckbSubMenu = new CheckBox ("Has sub-menus") {
  614. X = Pos.Left (_lblTitle),
  615. Y = Pos.Bottom (_ckbIsTopLevel),
  616. Checked = _menuItem == null ? !hasParent : HasSubMenus (_menuItem)
  617. };
  618. Add (_ckbSubMenu);
  619. _ckbNullCheck = new CheckBox ("Allow null checked") {
  620. X = Pos.Left (_lblTitle),
  621. Y = Pos.Bottom (_ckbSubMenu)
  622. };
  623. Add (_ckbNullCheck);
  624. var _rChkLabels = new string [] { "NoCheck", "Checked", "Radio" };
  625. _rbChkStyle = new RadioGroup (_rChkLabels) {
  626. X = Pos.Left (_lblTitle),
  627. Y = Pos.Bottom (_ckbSubMenu) + 1,
  628. };
  629. Add (_rbChkStyle);
  630. var _lblShortcut = new Label ("Shortcut:") {
  631. X = Pos.Right (_ckbSubMenu) + 10,
  632. Y = Pos.Top (_ckbSubMenu)
  633. };
  634. Add (_lblShortcut);
  635. _txtShortcut = new TextField () {
  636. X = Pos.X (_lblShortcut),
  637. Y = Pos.Bottom (_lblShortcut),
  638. Width = Dim.Fill (),
  639. ReadOnly = true
  640. };
  641. _txtShortcut.KeyDown += (s, e) => {
  642. if (!ProcessKey (e)) {
  643. return;
  644. }
  645. if (CheckShortcut (e.KeyCode, true)) {
  646. e.Handled = true;
  647. }
  648. };
  649. bool ProcessKey (Key ev)
  650. {
  651. switch (ev.KeyCode) {
  652. case KeyCode.CursorUp:
  653. case KeyCode.CursorDown:
  654. case KeyCode.Tab:
  655. case KeyCode.Tab | KeyCode.ShiftMask:
  656. return false;
  657. }
  658. return true;
  659. }
  660. bool CheckShortcut (KeyCode k, bool pre)
  661. {
  662. var m = _menuItem != null ? _menuItem : new MenuItem ();
  663. if (pre && !ShortcutHelper.PreShortcutValidation (k)) {
  664. _txtShortcut.Text = "";
  665. return false;
  666. }
  667. if (!pre) {
  668. if (!ShortcutHelper.PostShortcutValidation (ShortcutHelper.GetShortcutFromTag (_txtShortcut.Text))) {
  669. _txtShortcut.Text = "";
  670. return false;
  671. }
  672. return true;
  673. }
  674. _txtShortcut.Text = Key.ToString (k, MenuBar.ShortcutDelimiter);// ShortcutHelper.GetShortcutTag (k);
  675. return true;
  676. }
  677. _txtShortcut.KeyUp += (s, e) => {
  678. if (CheckShortcut (e.KeyCode, false)) {
  679. e.Handled = true;
  680. }
  681. };
  682. Add (_txtShortcut);
  683. var _btnShortcut = new Button ("Clear Shortcut") {
  684. X = Pos.X (_lblShortcut),
  685. Y = Pos.Bottom (_txtShortcut) + 1
  686. };
  687. _btnShortcut.Clicked += (s, e) => {
  688. _txtShortcut.Text = "";
  689. };
  690. Add (_btnShortcut);
  691. _ckbIsTopLevel.Toggled += (s, e) => {
  692. if ((_menuItem != null && _menuItem.Parent != null && (bool)_ckbIsTopLevel.Checked) ||
  693. _menuItem == null && hasParent && (bool)_ckbIsTopLevel.Checked) {
  694. MessageBox.ErrorQuery ("Invalid IsTopLevel", "Only menu bar can have top level menu item!", "Ok");
  695. _ckbIsTopLevel.Checked = false;
  696. return;
  697. }
  698. if (_ckbIsTopLevel.Checked == true) {
  699. _ckbSubMenu.Checked = false;
  700. _ckbSubMenu.SetNeedsDisplay ();
  701. _txtHelp.Enabled = true;
  702. _txtAction.Enabled = true;
  703. _txtShortcut.Enabled = _ckbIsTopLevel.Checked == false && _ckbSubMenu.Checked == false;
  704. } else {
  705. if (_menuItem == null && !hasParent || _menuItem.Parent == null) {
  706. _ckbSubMenu.Checked = true;
  707. _ckbSubMenu.SetNeedsDisplay ();
  708. _txtShortcut.Enabled = false;
  709. }
  710. _txtHelp.Text = "";
  711. _txtHelp.Enabled = false;
  712. _txtAction.Text = "";
  713. _txtAction.Enabled = false;
  714. }
  715. };
  716. _ckbSubMenu.Toggled += (s, e) => {
  717. if ((bool)_ckbSubMenu.Checked) {
  718. _ckbIsTopLevel.Checked = false;
  719. _ckbIsTopLevel.SetNeedsDisplay ();
  720. _txtHelp.Text = "";
  721. _txtHelp.Enabled = false;
  722. _txtAction.Text = "";
  723. _txtAction.Enabled = false;
  724. _txtShortcut.Text = "";
  725. _txtShortcut.Enabled = false;
  726. } else {
  727. if (!hasParent) {
  728. _ckbIsTopLevel.Checked = true;
  729. _ckbIsTopLevel.SetNeedsDisplay ();
  730. _txtShortcut.Enabled = false;
  731. }
  732. _txtHelp.Enabled = true;
  733. _txtAction.Enabled = true;
  734. _txtShortcut.Enabled = _ckbIsTopLevel.Checked == false && _ckbSubMenu.Checked == false;
  735. }
  736. };
  737. _ckbNullCheck.Toggled += (s, e) => {
  738. if (_menuItem != null) {
  739. _menuItem.AllowNullChecked = (bool)_ckbNullCheck.Checked;
  740. }
  741. };
  742. //Add (_frmMenuDetails);
  743. }
  744. public DynamicMenuItem EnterMenuItem ()
  745. {
  746. var valid = false;
  747. if (_menuItem == null) {
  748. var m = new DynamicMenuItem ();
  749. _txtTitle.Text = m.title;
  750. _txtHelp.Text = m.help;
  751. _txtAction.Text = m.action;
  752. _ckbIsTopLevel.Checked = false;
  753. _ckbSubMenu.Checked = !hasParent;
  754. _ckbNullCheck.Checked = false;
  755. _txtHelp.Enabled = hasParent;
  756. _txtAction.Enabled = hasParent;
  757. _txtShortcut.Enabled = hasParent;
  758. } else {
  759. EditMenuBarItem (_menuItem);
  760. }
  761. var _btnOk = new Button ("Ok") {
  762. IsDefault = true,
  763. };
  764. _btnOk.Clicked += (s, e) => {
  765. if (string.IsNullOrEmpty (_txtTitle.Text)) {
  766. MessageBox.ErrorQuery ("Invalid title", "Must enter a valid title!.", "Ok");
  767. } else {
  768. valid = true;
  769. Application.RequestStop ();
  770. }
  771. };
  772. var _btnCancel = new Button ("Cancel");
  773. _btnCancel.Clicked += (s, e) => {
  774. _txtTitle.Text = string.Empty;
  775. Application.RequestStop ();
  776. };
  777. var _dialog = new Dialog (_btnOk, _btnCancel) { Title = "Enter the menu details." };
  778. Width = Dim.Fill ();
  779. Height = Dim.Fill () - 1;
  780. _dialog.Add (this);
  781. _txtTitle.SetFocus ();
  782. _txtTitle.CursorPosition = _txtTitle.Text.Length;
  783. Application.Run (_dialog);
  784. if (valid) {
  785. return new DynamicMenuItem (_txtTitle.Text, _txtHelp.Text, _txtAction.Text,
  786. _ckbIsTopLevel != null ? (bool)_ckbIsTopLevel.Checked : false,
  787. _ckbSubMenu != null ? (bool)_ckbSubMenu.Checked : false,
  788. _rbChkStyle.SelectedItem == 0 ? MenuItemCheckStyle.NoCheck :
  789. _rbChkStyle.SelectedItem == 1 ? MenuItemCheckStyle.Checked : MenuItemCheckStyle.Radio,
  790. _txtShortcut.Text, (bool)_ckbNullCheck.Checked);
  791. } else {
  792. return null;
  793. }
  794. }
  795. public void EditMenuBarItem (MenuItem menuItem)
  796. {
  797. if (menuItem == null) {
  798. hasParent = false;
  799. Enabled = false;
  800. CleanEditMenuBarItem ();
  801. return;
  802. } else {
  803. hasParent = menuItem.Parent != null;
  804. Enabled = true;
  805. }
  806. _menuItem = menuItem;
  807. _txtTitle.Text = menuItem?.Title ?? "";
  808. _txtHelp.Text = menuItem?.Help ?? "";
  809. _txtAction.Text = menuItem != null && menuItem.Action != null ? GetTargetAction (menuItem.Action) : string.Empty;
  810. _ckbIsTopLevel.Checked = IsTopLevel (menuItem);
  811. _ckbSubMenu.Checked = HasSubMenus (menuItem);
  812. _ckbNullCheck.Checked = menuItem.AllowNullChecked;
  813. _txtHelp.Enabled = (bool)!_ckbSubMenu.Checked;
  814. _txtAction.Enabled = (bool)!_ckbSubMenu.Checked;
  815. _rbChkStyle.SelectedItem = (int)(menuItem?.CheckType ?? MenuItemCheckStyle.NoCheck);
  816. _txtShortcut.Text = menuItem?.ShortcutTag ?? "";
  817. _txtShortcut.Enabled = _ckbIsTopLevel.Checked == false && _ckbSubMenu.Checked == false;
  818. }
  819. void CleanEditMenuBarItem ()
  820. {
  821. _txtTitle.Text = "";
  822. _txtHelp.Text = "";
  823. _txtAction.Text = "";
  824. _ckbIsTopLevel.Checked = false;
  825. _ckbSubMenu.Checked = false;
  826. _rbChkStyle.SelectedItem = (int)MenuItemCheckStyle.NoCheck;
  827. _txtShortcut.Text = "";
  828. }
  829. string GetTargetAction (Action action)
  830. {
  831. var me = action.Target;
  832. if (me == null) {
  833. throw new ArgumentException ();
  834. }
  835. object v = new object ();
  836. foreach (var field in me.GetType ().GetFields ()) {
  837. if (field.Name == "item") {
  838. v = field.GetValue (me);
  839. }
  840. }
  841. return v == null || !(v is DynamicMenuItem item) ? string.Empty : item.action;
  842. }
  843. bool IsTopLevel (MenuItem menuItem)
  844. {
  845. var topLevel = menuItem as MenuBarItem;
  846. if (topLevel != null && topLevel.Parent == null && (topLevel.Children == null || topLevel.Children.Length == 0) && topLevel.Action != null) {
  847. return true;
  848. } else {
  849. return false;
  850. }
  851. }
  852. bool HasSubMenus (MenuItem menuItem)
  853. {
  854. var menuBarItem = menuItem as MenuBarItem;
  855. if (menuBarItem != null && menuBarItem.Children != null && (menuBarItem.Children.Length > 0 || menuBarItem.Action == null)) {
  856. return true;
  857. } else {
  858. return false;
  859. }
  860. }
  861. public Action CreateAction (MenuItem menuItem, DynamicMenuItem item)
  862. {
  863. switch (menuItem.CheckType) {
  864. case MenuItemCheckStyle.NoCheck:
  865. return new Action (() => MessageBox.ErrorQuery (item.title, item.action, "Ok"));
  866. case MenuItemCheckStyle.Checked:
  867. return new Action (menuItem.ToggleChecked);
  868. case MenuItemCheckStyle.Radio:
  869. break;
  870. }
  871. return new Action (() => {
  872. menuItem.Checked = true;
  873. var parent = menuItem?.Parent as MenuBarItem;
  874. if (parent != null) {
  875. var childrens = parent.Children;
  876. for (int i = 0; i < childrens.Length; i++) {
  877. var child = childrens [i];
  878. if (child != menuItem) {
  879. child.Checked = false;
  880. }
  881. }
  882. }
  883. });
  884. }
  885. public void UpdateParent (ref MenuItem menuItem)
  886. {
  887. var parent = menuItem.Parent as MenuBarItem;
  888. var idx = parent.GetChildrenIndex (menuItem);
  889. if (!(menuItem is MenuBarItem)) {
  890. menuItem = new MenuBarItem (menuItem.Title, new MenuItem [] { }, menuItem.Parent);
  891. if (idx > -1) {
  892. parent.Children [idx] = menuItem;
  893. }
  894. } else {
  895. menuItem = new MenuItem (menuItem.Title, menuItem.Help, CreateAction (menuItem, new DynamicMenuItem ()), null, menuItem.Parent);
  896. if (idx > -1) {
  897. parent.Children [idx] = menuItem;
  898. }
  899. }
  900. }
  901. }
  902. public class DynamicMenuItemModel : INotifyPropertyChanged {
  903. public event PropertyChangedEventHandler PropertyChanged;
  904. private string menuBar;
  905. private string parent;
  906. private List<DynamicMenuItemList> menus;
  907. public string MenuBar {
  908. get => menuBar;
  909. set {
  910. if (value != menuBar) {
  911. menuBar = value;
  912. PropertyChanged?.Invoke (this, new PropertyChangedEventArgs (GetPropertyName ()));
  913. }
  914. }
  915. }
  916. public string Parent {
  917. get => parent;
  918. set {
  919. if (value != parent) {
  920. parent = value;
  921. PropertyChanged?.Invoke (this, new PropertyChangedEventArgs (GetPropertyName ()));
  922. }
  923. }
  924. }
  925. public List<DynamicMenuItemList> Menus {
  926. get => menus;
  927. set {
  928. if (value != menus) {
  929. menus = value;
  930. PropertyChanged?.Invoke (this, new PropertyChangedEventArgs (GetPropertyName ()));
  931. }
  932. }
  933. }
  934. public DynamicMenuItemModel ()
  935. {
  936. Menus = new List<DynamicMenuItemList> ();
  937. }
  938. public string GetPropertyName ([CallerMemberName] string propertyName = null)
  939. {
  940. return propertyName;
  941. }
  942. }
  943. public interface IValueConverter {
  944. object Convert (object value, object parameter = null);
  945. }
  946. public class Binding {
  947. public View Target { get; private set; }
  948. public View Source { get; private set; }
  949. public string SourcePropertyName { get; private set; }
  950. public string TargetPropertyName { get; private set; }
  951. private object sourceDataContext;
  952. private PropertyInfo sourceBindingProperty;
  953. private IValueConverter valueConverter;
  954. public Binding (View source, string sourcePropertyName, View target, string targetPropertyName, IValueConverter valueConverter = null)
  955. {
  956. Target = target;
  957. Source = source;
  958. SourcePropertyName = sourcePropertyName;
  959. TargetPropertyName = targetPropertyName;
  960. sourceDataContext = Source.GetType ().GetProperty ("DataContext").GetValue (Source);
  961. sourceBindingProperty = sourceDataContext.GetType ().GetProperty (SourcePropertyName);
  962. this.valueConverter = valueConverter;
  963. UpdateTarget ();
  964. var notifier = ((INotifyPropertyChanged)sourceDataContext);
  965. if (notifier != null) {
  966. notifier.PropertyChanged += (s, e) => {
  967. if (e.PropertyName == SourcePropertyName) {
  968. UpdateTarget ();
  969. }
  970. };
  971. }
  972. }
  973. private void UpdateTarget ()
  974. {
  975. try {
  976. var sourceValue = sourceBindingProperty.GetValue (sourceDataContext);
  977. if (sourceValue == null) {
  978. return;
  979. }
  980. var finalValue = valueConverter?.Convert (sourceValue) ?? sourceValue;
  981. var targetProperty = Target.GetType ().GetProperty (TargetPropertyName);
  982. targetProperty.SetValue (Target, finalValue);
  983. } catch (Exception ex) {
  984. MessageBox.ErrorQuery ("Binding Error", $"Binding failed: {ex}.", "Ok");
  985. }
  986. }
  987. }
  988. public class ListWrapperConverter : IValueConverter {
  989. public object Convert (object value, object parameter = null)
  990. {
  991. return new ListWrapper ((IList)value);
  992. }
  993. }
  994. public class UStringValueConverter : IValueConverter {
  995. public object Convert (object value, object parameter = null)
  996. {
  997. var data = Encoding.ASCII.GetBytes (value.ToString ());
  998. return StringExtensions.ToString (data);
  999. }
  1000. }
  1001. }
  1002. }