DynamicMenuBar.cs 36 KB

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