DynamicMenuBar.cs 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129
  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) {
  74. X = Pos.Center (),
  75. Width = 2,
  76. };
  77. _txtDelimiter.TextChanged += (s, _) => MenuBar.ShortcutDelimiter = _txtDelimiter.Text;
  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.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.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.KeyEvent)) {
  643. return;
  644. }
  645. var k = ShortcutHelper.GetModifiersKey (e.KeyEvent);
  646. if (CheckShortcut (k, true)) {
  647. e.Handled = true;
  648. }
  649. };
  650. bool ProcessKey (KeyEvent ev)
  651. {
  652. switch (ev.Key) {
  653. case Key.CursorUp:
  654. case Key.CursorDown:
  655. case Key.Tab:
  656. case Key.BackTab:
  657. return false;
  658. }
  659. return true;
  660. }
  661. bool CheckShortcut (Key k, bool pre)
  662. {
  663. var m = _menuItem != null ? _menuItem : new MenuItem ();
  664. if (pre && !ShortcutHelper.PreShortcutValidation (k)) {
  665. _txtShortcut.Text = "";
  666. return false;
  667. }
  668. if (!pre) {
  669. if (!ShortcutHelper.PostShortcutValidation (ShortcutHelper.GetShortcutFromTag (_txtShortcut.Text))) {
  670. _txtShortcut.Text = "";
  671. return false;
  672. }
  673. return true;
  674. }
  675. _txtShortcut.Text = ShortcutHelper.GetShortcutTag (k);
  676. return true;
  677. }
  678. _txtShortcut.KeyUp += (s, e) => {
  679. var k = ShortcutHelper.GetModifiersKey (e.KeyEvent);
  680. if (CheckShortcut (k, false)) {
  681. e.Handled = true;
  682. }
  683. };
  684. Add (_txtShortcut);
  685. var _btnShortcut = new Button ("Clear Shortcut") {
  686. X = Pos.X (_lblShortcut),
  687. Y = Pos.Bottom (_txtShortcut) + 1
  688. };
  689. _btnShortcut.Clicked += (s, e) => {
  690. _txtShortcut.Text = "";
  691. };
  692. Add (_btnShortcut);
  693. _ckbIsTopLevel.Toggled += (s, e) => {
  694. if ((_menuItem != null && _menuItem.Parent != null && (bool)_ckbIsTopLevel.Checked) ||
  695. _menuItem == null && hasParent && (bool)_ckbIsTopLevel.Checked) {
  696. MessageBox.ErrorQuery ("Invalid IsTopLevel", "Only menu bar can have top level menu item!", "Ok");
  697. _ckbIsTopLevel.Checked = false;
  698. return;
  699. }
  700. if (_ckbIsTopLevel.Checked == true) {
  701. _ckbSubMenu.Checked = false;
  702. _ckbSubMenu.SetNeedsDisplay ();
  703. _txtHelp.Enabled = true;
  704. _txtAction.Enabled = true;
  705. _txtShortcut.Enabled = _ckbIsTopLevel.Checked == false && _ckbSubMenu.Checked == false;
  706. } else {
  707. if (_menuItem == null && !hasParent || _menuItem.Parent == null) {
  708. _ckbSubMenu.Checked = true;
  709. _ckbSubMenu.SetNeedsDisplay ();
  710. _txtShortcut.Enabled = false;
  711. }
  712. _txtHelp.Text = "";
  713. _txtHelp.Enabled = false;
  714. _txtAction.Text = "";
  715. _txtAction.Enabled = false;
  716. }
  717. };
  718. _ckbSubMenu.Toggled += (s, e) => {
  719. if ((bool)_ckbSubMenu.Checked) {
  720. _ckbIsTopLevel.Checked = false;
  721. _ckbIsTopLevel.SetNeedsDisplay ();
  722. _txtHelp.Text = "";
  723. _txtHelp.Enabled = false;
  724. _txtAction.Text = "";
  725. _txtAction.Enabled = false;
  726. _txtShortcut.Text = "";
  727. _txtShortcut.Enabled = false;
  728. } else {
  729. if (!hasParent) {
  730. _ckbIsTopLevel.Checked = true;
  731. _ckbIsTopLevel.SetNeedsDisplay ();
  732. _txtShortcut.Enabled = false;
  733. }
  734. _txtHelp.Enabled = true;
  735. _txtAction.Enabled = true;
  736. _txtShortcut.Enabled = _ckbIsTopLevel.Checked == false && _ckbSubMenu.Checked == false;
  737. }
  738. };
  739. _ckbNullCheck.Toggled += (s, e) => {
  740. if (_menuItem != null) {
  741. _menuItem.AllowNullChecked = (bool)_ckbNullCheck.Checked;
  742. }
  743. };
  744. //Add (_frmMenuDetails);
  745. }
  746. public DynamicMenuItem EnterMenuItem ()
  747. {
  748. var valid = false;
  749. if (_menuItem == null) {
  750. var m = new DynamicMenuItem ();
  751. _txtTitle.Text = m.title;
  752. _txtHelp.Text = m.help;
  753. _txtAction.Text = m.action;
  754. _ckbIsTopLevel.Checked = false;
  755. _ckbSubMenu.Checked = !hasParent;
  756. _ckbNullCheck.Checked = false;
  757. _txtHelp.Enabled = hasParent;
  758. _txtAction.Enabled = hasParent;
  759. _txtShortcut.Enabled = hasParent;
  760. } else {
  761. EditMenuBarItem (_menuItem);
  762. }
  763. var _btnOk = new Button ("Ok") {
  764. IsDefault = true,
  765. };
  766. _btnOk.Clicked += (s, e) => {
  767. if (string.IsNullOrEmpty (_txtTitle.Text)) {
  768. MessageBox.ErrorQuery ("Invalid title", "Must enter a valid title!.", "Ok");
  769. } else {
  770. valid = true;
  771. Application.RequestStop ();
  772. }
  773. };
  774. var _btnCancel = new Button ("Cancel");
  775. _btnCancel.Clicked += (s, e) => {
  776. _txtTitle.Text = string.Empty;
  777. Application.RequestStop ();
  778. };
  779. var _dialog = new Dialog (_btnOk, _btnCancel) { Title = "Enter the menu details." };
  780. Width = Dim.Fill ();
  781. Height = Dim.Fill () - 1;
  782. _dialog.Add (this);
  783. _txtTitle.SetFocus ();
  784. _txtTitle.CursorPosition = _txtTitle.Text.Length;
  785. Application.Run (_dialog);
  786. if (valid) {
  787. return new DynamicMenuItem (_txtTitle.Text, _txtHelp.Text, _txtAction.Text,
  788. _ckbIsTopLevel != null ? (bool)_ckbIsTopLevel.Checked : false,
  789. _ckbSubMenu != null ? (bool)_ckbSubMenu.Checked : false,
  790. _rbChkStyle.SelectedItem == 0 ? MenuItemCheckStyle.NoCheck :
  791. _rbChkStyle.SelectedItem == 1 ? MenuItemCheckStyle.Checked : MenuItemCheckStyle.Radio,
  792. _txtShortcut.Text, (bool)_ckbNullCheck.Checked);
  793. } else {
  794. return null;
  795. }
  796. }
  797. public void EditMenuBarItem (MenuItem menuItem)
  798. {
  799. if (menuItem == null) {
  800. hasParent = false;
  801. Enabled = false;
  802. CleanEditMenuBarItem ();
  803. return;
  804. } else {
  805. hasParent = menuItem.Parent != null;
  806. Enabled = true;
  807. }
  808. _menuItem = menuItem;
  809. _txtTitle.Text = menuItem?.Title ?? "";
  810. _txtHelp.Text = menuItem?.Help ?? "";
  811. _txtAction.Text = menuItem != null && menuItem.Action != null ? GetTargetAction (menuItem.Action) : string.Empty;
  812. _ckbIsTopLevel.Checked = IsTopLevel (menuItem);
  813. _ckbSubMenu.Checked = HasSubMenus (menuItem);
  814. _ckbNullCheck.Checked = menuItem.AllowNullChecked;
  815. _txtHelp.Enabled = (bool)!_ckbSubMenu.Checked;
  816. _txtAction.Enabled = (bool)!_ckbSubMenu.Checked;
  817. _rbChkStyle.SelectedItem = (int)(menuItem?.CheckType ?? MenuItemCheckStyle.NoCheck);
  818. _txtShortcut.Text = menuItem?.ShortcutTag ?? "";
  819. _txtShortcut.Enabled = _ckbIsTopLevel.Checked == false && _ckbSubMenu.Checked == false;
  820. }
  821. void CleanEditMenuBarItem ()
  822. {
  823. _txtTitle.Text = "";
  824. _txtHelp.Text = "";
  825. _txtAction.Text = "";
  826. _ckbIsTopLevel.Checked = false;
  827. _ckbSubMenu.Checked = false;
  828. _rbChkStyle.SelectedItem = (int)MenuItemCheckStyle.NoCheck;
  829. _txtShortcut.Text = "";
  830. }
  831. string GetTargetAction (Action action)
  832. {
  833. var me = action.Target;
  834. if (me == null) {
  835. throw new ArgumentException ();
  836. }
  837. object v = new object ();
  838. foreach (var field in me.GetType ().GetFields ()) {
  839. if (field.Name == "item") {
  840. v = field.GetValue (me);
  841. }
  842. }
  843. return v == null || !(v is DynamicMenuItem item) ? string.Empty : item.action;
  844. }
  845. bool IsTopLevel (MenuItem menuItem)
  846. {
  847. var topLevel = menuItem as MenuBarItem;
  848. if (topLevel != null && topLevel.Parent == null && (topLevel.Children == null || topLevel.Children.Length == 0) && topLevel.Action != null) {
  849. return true;
  850. } else {
  851. return false;
  852. }
  853. }
  854. bool HasSubMenus (MenuItem menuItem)
  855. {
  856. var menuBarItem = menuItem as MenuBarItem;
  857. if (menuBarItem != null && menuBarItem.Children != null && (menuBarItem.Children.Length > 0 || menuBarItem.Action == null)) {
  858. return true;
  859. } else {
  860. return false;
  861. }
  862. }
  863. public Action CreateAction (MenuItem menuItem, DynamicMenuItem item)
  864. {
  865. switch (menuItem.CheckType) {
  866. case MenuItemCheckStyle.NoCheck:
  867. return new Action (() => MessageBox.ErrorQuery (item.title, item.action, "Ok"));
  868. case MenuItemCheckStyle.Checked:
  869. return new Action (menuItem.ToggleChecked);
  870. case MenuItemCheckStyle.Radio:
  871. break;
  872. }
  873. return new Action (() => {
  874. menuItem.Checked = true;
  875. var parent = menuItem?.Parent as MenuBarItem;
  876. if (parent != null) {
  877. var childrens = parent.Children;
  878. for (int i = 0; i < childrens.Length; i++) {
  879. var child = childrens [i];
  880. if (child != menuItem) {
  881. child.Checked = false;
  882. }
  883. }
  884. }
  885. });
  886. }
  887. public void UpdateParent (ref MenuItem menuItem)
  888. {
  889. var parent = menuItem.Parent as MenuBarItem;
  890. var idx = parent.GetChildrenIndex (menuItem);
  891. if (!(menuItem is MenuBarItem)) {
  892. menuItem = new MenuBarItem (menuItem.Title, new MenuItem [] { }, menuItem.Parent);
  893. if (idx > -1) {
  894. parent.Children [idx] = menuItem;
  895. }
  896. } else {
  897. menuItem = new MenuItem (menuItem.Title, menuItem.Help, CreateAction (menuItem, new DynamicMenuItem ()), null, menuItem.Parent);
  898. if (idx > -1) {
  899. parent.Children [idx] = menuItem;
  900. }
  901. }
  902. }
  903. }
  904. public class DynamicMenuItemModel : INotifyPropertyChanged {
  905. public event PropertyChangedEventHandler PropertyChanged;
  906. private string menuBar;
  907. private string parent;
  908. private List<DynamicMenuItemList> menus;
  909. public string MenuBar {
  910. get => menuBar;
  911. set {
  912. if (value != menuBar) {
  913. menuBar = value;
  914. PropertyChanged?.Invoke (this, new PropertyChangedEventArgs (GetPropertyName ()));
  915. }
  916. }
  917. }
  918. public string Parent {
  919. get => parent;
  920. set {
  921. if (value != parent) {
  922. parent = value;
  923. PropertyChanged?.Invoke (this, new PropertyChangedEventArgs (GetPropertyName ()));
  924. }
  925. }
  926. }
  927. public List<DynamicMenuItemList> Menus {
  928. get => menus;
  929. set {
  930. if (value != menus) {
  931. menus = value;
  932. PropertyChanged?.Invoke (this, new PropertyChangedEventArgs (GetPropertyName ()));
  933. }
  934. }
  935. }
  936. public DynamicMenuItemModel ()
  937. {
  938. Menus = new List<DynamicMenuItemList> ();
  939. }
  940. public string GetPropertyName ([CallerMemberName] string propertyName = null)
  941. {
  942. return propertyName;
  943. }
  944. }
  945. public interface IValueConverter {
  946. object Convert (object value, object parameter = null);
  947. }
  948. public class Binding {
  949. public View Target { get; private set; }
  950. public View Source { get; private set; }
  951. public string SourcePropertyName { get; private set; }
  952. public string TargetPropertyName { get; private set; }
  953. private object sourceDataContext;
  954. private PropertyInfo sourceBindingProperty;
  955. private IValueConverter valueConverter;
  956. public Binding (View source, string sourcePropertyName, View target, string targetPropertyName, IValueConverter valueConverter = null)
  957. {
  958. Target = target;
  959. Source = source;
  960. SourcePropertyName = sourcePropertyName;
  961. TargetPropertyName = targetPropertyName;
  962. sourceDataContext = Source.GetType ().GetProperty ("DataContext").GetValue (Source);
  963. sourceBindingProperty = sourceDataContext.GetType ().GetProperty (SourcePropertyName);
  964. this.valueConverter = valueConverter;
  965. UpdateTarget ();
  966. var notifier = ((INotifyPropertyChanged)sourceDataContext);
  967. if (notifier != null) {
  968. notifier.PropertyChanged += (s, e) => {
  969. if (e.PropertyName == SourcePropertyName) {
  970. UpdateTarget ();
  971. }
  972. };
  973. }
  974. }
  975. private void UpdateTarget ()
  976. {
  977. try {
  978. var sourceValue = sourceBindingProperty.GetValue (sourceDataContext);
  979. if (sourceValue == null) {
  980. return;
  981. }
  982. var finalValue = valueConverter?.Convert (sourceValue) ?? sourceValue;
  983. var targetProperty = Target.GetType ().GetProperty (TargetPropertyName);
  984. targetProperty.SetValue (Target, finalValue);
  985. } catch (Exception ex) {
  986. MessageBox.ErrorQuery ("Binding Error", $"Binding failed: {ex}.", "Ok");
  987. }
  988. }
  989. }
  990. public class ListWrapperConverter : IValueConverter {
  991. public object Convert (object value, object parameter = null)
  992. {
  993. return new ListWrapper ((IList)value);
  994. }
  995. }
  996. public class UStringValueConverter : IValueConverter {
  997. public object Convert (object value, object parameter = null)
  998. {
  999. var data = Encoding.ASCII.GetBytes (value.ToString ());
  1000. return StringExtensions.ToString (data);
  1001. }
  1002. }
  1003. }
  1004. }