DynamicStatusBar.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Reflection;
  6. using System.Runtime.CompilerServices;
  7. using System.Text;
  8. using Terminal.Gui;
  9. namespace UICatalog.Scenarios;
  10. [ScenarioMetadata ("Dynamic StatusBar", "Demonstrates how to add and remove a StatusBar and change items dynamically.")]
  11. [ScenarioCategory ("Top Level Windows")]
  12. public class DynamicStatusBar : Scenario
  13. {
  14. public override void Init ()
  15. {
  16. Application.Init ();
  17. Top = new ();
  18. Top.Add (
  19. new DynamicStatusBarSample { Title = $"{Application.QuitKey} to Quit - Scenario: {GetName ()}" }
  20. );
  21. }
  22. public class Binding
  23. {
  24. private readonly PropertyInfo _sourceBindingProperty;
  25. private readonly object _sourceDataContext;
  26. private readonly IValueConverter _valueConverter;
  27. public Binding (
  28. View source,
  29. string sourcePropertyName,
  30. View target,
  31. string targetPropertyName,
  32. IValueConverter valueConverter = null
  33. )
  34. {
  35. Target = target;
  36. Source = source;
  37. SourcePropertyName = sourcePropertyName;
  38. TargetPropertyName = targetPropertyName;
  39. _sourceDataContext = Source.GetType ().GetProperty ("DataContext").GetValue (Source);
  40. _sourceBindingProperty = _sourceDataContext.GetType ().GetProperty (SourcePropertyName);
  41. _valueConverter = valueConverter;
  42. UpdateTarget ();
  43. var notifier = (INotifyPropertyChanged)_sourceDataContext;
  44. if (notifier != null)
  45. {
  46. notifier.PropertyChanged += (s, e) =>
  47. {
  48. if (e.PropertyName == SourcePropertyName)
  49. {
  50. UpdateTarget ();
  51. }
  52. };
  53. }
  54. }
  55. public View Source { get; }
  56. public string SourcePropertyName { get; }
  57. public View Target { get; }
  58. public string TargetPropertyName { get; }
  59. private void UpdateTarget ()
  60. {
  61. try
  62. {
  63. object sourceValue = _sourceBindingProperty.GetValue (_sourceDataContext);
  64. if (sourceValue == null)
  65. {
  66. return;
  67. }
  68. object finalValue = _valueConverter?.Convert (sourceValue) ?? sourceValue;
  69. PropertyInfo targetProperty = Target.GetType ().GetProperty (TargetPropertyName);
  70. targetProperty.SetValue (Target, finalValue);
  71. }
  72. catch (Exception ex)
  73. {
  74. MessageBox.ErrorQuery ("Binding Error", $"Binding failed: {ex}.", "Ok");
  75. }
  76. }
  77. }
  78. public class DynamicStatusBarDetails : FrameView
  79. {
  80. private StatusItem _statusItem;
  81. public DynamicStatusBarDetails (StatusItem statusItem = null) : this ()
  82. {
  83. _statusItem = statusItem;
  84. Title = statusItem == null ? "Adding New StatusBar Item." : "Editing StatusBar Item.";
  85. }
  86. public DynamicStatusBarDetails ()
  87. {
  88. var _lblTitle = new Label { Y = 1, Text = "Title:" };
  89. Add (_lblTitle);
  90. TextTitle = new TextField { X = Pos.Right (_lblTitle) + 4, Y = Pos.Top (_lblTitle), Width = Dim.Fill () };
  91. Add (TextTitle);
  92. var _lblAction = new Label { X = Pos.Left (_lblTitle), Y = Pos.Bottom (_lblTitle) + 1, Text = "Action:" };
  93. Add (_lblAction);
  94. TextAction = new TextView
  95. {
  96. X = Pos.Left (TextTitle), Y = Pos.Top (_lblAction), Width = Dim.Fill (), Height = 5
  97. };
  98. Add (TextAction);
  99. var _lblShortcut = new Label
  100. {
  101. X = Pos.Left (_lblTitle), Y = Pos.Bottom (TextAction) + 1, Text = "Shortcut:"
  102. };
  103. Add (_lblShortcut);
  104. TextShortcut = new TextField
  105. {
  106. X = Pos.X (TextAction), Y = Pos.Y (_lblShortcut), Width = Dim.Fill (), ReadOnly = true
  107. };
  108. TextShortcut.KeyDown += (s, e) =>
  109. {
  110. if (!ProcessKey (e))
  111. {
  112. return;
  113. }
  114. if (CheckShortcut (e.KeyCode, true))
  115. {
  116. e.Handled = true;
  117. }
  118. };
  119. bool ProcessKey (Key ev)
  120. {
  121. switch (ev.KeyCode)
  122. {
  123. case KeyCode.CursorUp:
  124. case KeyCode.CursorDown:
  125. case KeyCode.Tab:
  126. case KeyCode.Tab | KeyCode.ShiftMask:
  127. return false;
  128. }
  129. return true;
  130. }
  131. bool CheckShortcut (KeyCode k, bool pre)
  132. {
  133. StatusItem m = _statusItem != null ? _statusItem : new StatusItem (k, "", null);
  134. if (pre && !ShortcutHelper.PreShortcutValidation (k))
  135. {
  136. TextShortcut.Text = "";
  137. return false;
  138. }
  139. if (!pre)
  140. {
  141. if (!ShortcutHelper.PostShortcutValidation (
  142. ShortcutHelper.GetShortcutFromTag (
  143. TextShortcut.Text,
  144. StatusBar.ShortcutDelimiter
  145. )
  146. ))
  147. {
  148. TextShortcut.Text = "";
  149. return false;
  150. }
  151. return true;
  152. }
  153. TextShortcut.Text =
  154. Key.ToString (
  155. k,
  156. StatusBar
  157. .ShortcutDelimiter
  158. ); //ShortcutHelper.GetShortcutTag (k, StatusBar.ShortcutDelimiter);
  159. return true;
  160. }
  161. TextShortcut.KeyUp += (s, e) =>
  162. {
  163. if (CheckShortcut (e.KeyCode, true))
  164. {
  165. e.Handled = true;
  166. }
  167. };
  168. Add (TextShortcut);
  169. var _btnShortcut = new Button
  170. {
  171. X = Pos.X (_lblShortcut), Y = Pos.Bottom (TextShortcut) + 1, Text = "Clear Shortcut"
  172. };
  173. _btnShortcut.Accept += (s, e) => { TextShortcut.Text = ""; };
  174. Add (_btnShortcut);
  175. }
  176. public TextView TextAction { get; }
  177. public TextField TextShortcut { get; }
  178. public TextField TextTitle { get; }
  179. public Action CreateAction (DynamicStatusItem item) { return () => MessageBox.ErrorQuery (item.Title, item.Action, "Ok"); }
  180. public void EditStatusItem (StatusItem statusItem)
  181. {
  182. if (statusItem == null)
  183. {
  184. Enabled = false;
  185. CleanEditStatusItem ();
  186. return;
  187. }
  188. Enabled = true;
  189. _statusItem = statusItem;
  190. TextTitle.Text = statusItem?.Title ?? "";
  191. TextAction.Text = statusItem != null && statusItem.Action != null
  192. ? GetTargetAction (statusItem.Action)
  193. : string.Empty;
  194. TextShortcut.Text =
  195. Key.ToString (
  196. (KeyCode)statusItem.Shortcut,
  197. StatusBar
  198. .ShortcutDelimiter
  199. ); //ShortcutHelper.GetShortcutTag (statusItem.Shortcut, StatusBar.ShortcutDelimiter) ?? "";
  200. }
  201. public DynamicStatusItem EnterStatusItem ()
  202. {
  203. var valid = false;
  204. if (_statusItem == null)
  205. {
  206. var m = new DynamicStatusItem ();
  207. TextTitle.Text = m.Title;
  208. TextAction.Text = m.Action;
  209. }
  210. else
  211. {
  212. EditStatusItem (_statusItem);
  213. }
  214. var btnOk = new Button { IsDefault = true, Text = "OK" };
  215. btnOk.Accept += (s, e) =>
  216. {
  217. if (string.IsNullOrEmpty (TextTitle.Text))
  218. {
  219. MessageBox.ErrorQuery ("Invalid title", "Must enter a valid title!.", "Ok");
  220. }
  221. else
  222. {
  223. if (!string.IsNullOrEmpty (TextShortcut.Text))
  224. {
  225. TextTitle.Text = DynamicStatusBarSample.SetTitleText (
  226. TextTitle.Text,
  227. TextShortcut.Text
  228. );
  229. }
  230. valid = true;
  231. Application.RequestStop ();
  232. }
  233. };
  234. var btnCancel = new Button { Text = "Cancel" };
  235. btnCancel.Accept += (s, e) =>
  236. {
  237. TextTitle.Text = string.Empty;
  238. Application.RequestStop ();
  239. };
  240. var dialog = new Dialog { Title = "Enter the menu details.", Buttons = [btnOk, btnCancel] };
  241. Width = Dim.Fill ();
  242. Height = Dim.Fill () - 1;
  243. dialog.Add (this);
  244. TextTitle.SetFocus ();
  245. TextTitle.CursorPosition = TextTitle.Text.Length;
  246. Application.Run (dialog);
  247. dialog.Dispose ();
  248. return valid
  249. ? new DynamicStatusItem
  250. {
  251. Title = TextTitle.Text, Action = TextAction.Text, Shortcut = TextShortcut.Text
  252. }
  253. : null;
  254. }
  255. private void CleanEditStatusItem ()
  256. {
  257. TextTitle.Text = "";
  258. TextAction.Text = "";
  259. TextShortcut.Text = "";
  260. }
  261. private string GetTargetAction (Action action)
  262. {
  263. object me = action.Target;
  264. if (me == null)
  265. {
  266. throw new ArgumentException ();
  267. }
  268. var v = new object ();
  269. foreach (FieldInfo field in me.GetType ().GetFields ())
  270. {
  271. if (field.Name == "item")
  272. {
  273. v = field.GetValue (me);
  274. }
  275. }
  276. return v == null || !(v is DynamicStatusItem item) ? string.Empty : item.Action;
  277. }
  278. }
  279. public class DynamicStatusBarSample : Window
  280. {
  281. private readonly ListView _lstItems;
  282. private StatusItem _currentEditStatusItem;
  283. private int _currentSelectedStatusBar = -1;
  284. private StatusItem _currentStatusItem;
  285. private StatusBar _statusBar;
  286. public DynamicStatusBarSample ()
  287. {
  288. DataContext = new DynamicStatusItemModel ();
  289. var _frmDelimiter = new FrameView
  290. {
  291. X = Pos.Center (),
  292. Y = 0,
  293. Width = 25,
  294. Height = 4,
  295. Title = "Shortcut Delimiter:"
  296. };
  297. var _txtDelimiter = new TextField { X = Pos.Center (), Width = 2, Text = $"{StatusBar.ShortcutDelimiter}" };
  298. _txtDelimiter.TextChanged += (s, _) =>
  299. StatusBar.ShortcutDelimiter = _txtDelimiter.Text.ToRunes () [0];
  300. _frmDelimiter.Add (_txtDelimiter);
  301. Add (_frmDelimiter);
  302. var _frmStatusBar = new FrameView
  303. {
  304. Y = 5, Width = Dim.Percent (50), Height = Dim.Fill (2), Title = "Items:"
  305. };
  306. var _btnAddStatusBar = new Button { Y = 1, Text = "Add a StatusBar" };
  307. _frmStatusBar.Add (_btnAddStatusBar);
  308. var _btnRemoveStatusBar = new Button { Y = 1, Text = "Remove a StatusBar" };
  309. _btnRemoveStatusBar.X = Pos.AnchorEnd () - (Pos.Right (_btnRemoveStatusBar) - Pos.Left (_btnRemoveStatusBar));
  310. _frmStatusBar.Add (_btnRemoveStatusBar);
  311. var _btnAdd = new Button { Y = Pos.Top (_btnRemoveStatusBar) + 2, Text = " Add " };
  312. _btnAdd.X = Pos.AnchorEnd () - (Pos.Right (_btnAdd) - Pos.Left (_btnAdd));
  313. _frmStatusBar.Add (_btnAdd);
  314. _lstItems = new ListView
  315. {
  316. ColorScheme = Colors.ColorSchemes ["Dialog"],
  317. Y = Pos.Top (_btnAddStatusBar) + 2,
  318. Width = Dim.Fill () - Dim.Width (_btnAdd) - 1,
  319. Height = Dim.Fill (),
  320. Source = new ListWrapper (new List<DynamicStatusItemList> ())
  321. };
  322. _frmStatusBar.Add (_lstItems);
  323. var _btnRemove = new Button { X = Pos.Left (_btnAdd), Y = Pos.Top (_btnAdd) + 1, Text = "Remove" };
  324. _frmStatusBar.Add (_btnRemove);
  325. var _btnUp = new Button { X = Pos.Right (_lstItems) + 2, Y = Pos.Top (_btnRemove) + 2, Text = "^" };
  326. _frmStatusBar.Add (_btnUp);
  327. var _btnDown = new Button { X = Pos.Right (_lstItems) + 2, Y = Pos.Top (_btnUp) + 1, Text = "v" };
  328. _frmStatusBar.Add (_btnDown);
  329. Add (_frmStatusBar);
  330. var _frmStatusBarDetails = new DynamicStatusBarDetails
  331. {
  332. X = Pos.Right (_frmStatusBar),
  333. Y = Pos.Top (_frmStatusBar),
  334. Width = Dim.Fill (),
  335. Height = Dim.Fill (4),
  336. Title = "StatusBar Item Details:"
  337. };
  338. Add (_frmStatusBarDetails);
  339. _btnUp.Accept += (s, e) =>
  340. {
  341. int i = _lstItems.SelectedItem;
  342. StatusItem statusItem = DataContext.Items.Count > 0 ? DataContext.Items [i].StatusItem : null;
  343. if (statusItem != null)
  344. {
  345. StatusItem [] items = _statusBar.Items;
  346. if (i > 0)
  347. {
  348. items [i] = items [i - 1];
  349. items [i - 1] = statusItem;
  350. DataContext.Items [i] = DataContext.Items [i - 1];
  351. DataContext.Items [i - 1] =
  352. new DynamicStatusItemList (statusItem.Title, statusItem);
  353. _lstItems.SelectedItem = i - 1;
  354. _statusBar.SetNeedsDisplay ();
  355. }
  356. }
  357. };
  358. _btnDown.Accept += (s, e) =>
  359. {
  360. int i = _lstItems.SelectedItem;
  361. StatusItem statusItem = DataContext.Items.Count > 0 ? DataContext.Items [i].StatusItem : null;
  362. if (statusItem != null)
  363. {
  364. StatusItem [] items = _statusBar.Items;
  365. if (i < items.Length - 1)
  366. {
  367. items [i] = items [i + 1];
  368. items [i + 1] = statusItem;
  369. DataContext.Items [i] = DataContext.Items [i + 1];
  370. DataContext.Items [i + 1] =
  371. new DynamicStatusItemList (statusItem.Title, statusItem);
  372. _lstItems.SelectedItem = i + 1;
  373. _statusBar.SetNeedsDisplay ();
  374. }
  375. }
  376. };
  377. var _btnOk = new Button
  378. {
  379. X = Pos.Right (_frmStatusBar) + 20, Y = Pos.Bottom (_frmStatusBarDetails), Text = "Ok"
  380. };
  381. Add (_btnOk);
  382. var _btnCancel = new Button { X = Pos.Right (_btnOk) + 3, Y = Pos.Top (_btnOk), Text = "Cancel" };
  383. _btnCancel.Accept += (s, e) => { SetFrameDetails (_currentEditStatusItem); };
  384. Add (_btnCancel);
  385. _lstItems.SelectedItemChanged += (s, e) => { SetFrameDetails (); };
  386. _btnOk.Accept += (s, e) =>
  387. {
  388. if (string.IsNullOrEmpty (_frmStatusBarDetails.TextTitle.Text) && _currentEditStatusItem != null)
  389. {
  390. MessageBox.ErrorQuery ("Invalid title", "Must enter a valid title!.", "Ok");
  391. }
  392. else if (_currentEditStatusItem != null)
  393. {
  394. _frmStatusBarDetails.TextTitle.Text = SetTitleText (
  395. _frmStatusBarDetails.TextTitle.Text,
  396. _frmStatusBarDetails.TextShortcut.Text
  397. );
  398. var statusItem = new DynamicStatusItem
  399. {
  400. Title = _frmStatusBarDetails.TextTitle.Text,
  401. Action = _frmStatusBarDetails.TextAction.Text,
  402. Shortcut = _frmStatusBarDetails.TextShortcut.Text
  403. };
  404. UpdateStatusItem (_currentEditStatusItem, statusItem, _lstItems.SelectedItem);
  405. }
  406. };
  407. _btnAdd.Accept += (s, e) =>
  408. {
  409. if (StatusBar == null)
  410. {
  411. MessageBox.ErrorQuery (
  412. "StatusBar Bar Error",
  413. "Must add a StatusBar first!",
  414. "Ok"
  415. );
  416. _btnAddStatusBar.SetFocus ();
  417. return;
  418. }
  419. var frameDetails = new DynamicStatusBarDetails ();
  420. DynamicStatusItem item = frameDetails.EnterStatusItem ();
  421. if (item == null)
  422. {
  423. return;
  424. }
  425. StatusItem newStatusItem = CreateNewStatusBar (item);
  426. _currentSelectedStatusBar++;
  427. _statusBar.AddItemAt (_currentSelectedStatusBar, newStatusItem);
  428. DataContext.Items.Add (new DynamicStatusItemList (newStatusItem.Title, newStatusItem));
  429. _lstItems.MoveDown ();
  430. SetFrameDetails ();
  431. };
  432. _btnRemove.Accept += (s, e) =>
  433. {
  434. StatusItem statusItem = DataContext.Items.Count > 0
  435. ? DataContext.Items [_lstItems.SelectedItem].StatusItem
  436. : null;
  437. if (statusItem != null)
  438. {
  439. _statusBar.RemoveItem (_currentSelectedStatusBar);
  440. DataContext.Items.RemoveAt (_lstItems.SelectedItem);
  441. if (_lstItems.Source.Count > 0 && _lstItems.SelectedItem > _lstItems.Source.Count - 1)
  442. {
  443. _lstItems.SelectedItem = _lstItems.Source.Count - 1;
  444. }
  445. _lstItems.SetNeedsDisplay ();
  446. SetFrameDetails ();
  447. }
  448. };
  449. _lstItems.Enter += (s, e) =>
  450. {
  451. StatusItem statusItem = DataContext.Items.Count > 0
  452. ? DataContext.Items [_lstItems.SelectedItem].StatusItem
  453. : null;
  454. SetFrameDetails (statusItem);
  455. };
  456. _btnAddStatusBar.Accept += (s, e) =>
  457. {
  458. if (_statusBar != null)
  459. {
  460. return;
  461. }
  462. _statusBar = new StatusBar ();
  463. Add (_statusBar);
  464. };
  465. _btnRemoveStatusBar.Accept += (s, e) =>
  466. {
  467. if (_statusBar == null)
  468. {
  469. return;
  470. }
  471. Remove (_statusBar);
  472. _statusBar = null;
  473. DataContext.Items = new List<DynamicStatusItemList> ();
  474. _currentStatusItem = null;
  475. _currentSelectedStatusBar = -1;
  476. SetListViewSource (_currentStatusItem, true);
  477. SetFrameDetails ();
  478. };
  479. SetFrameDetails ();
  480. var ustringConverter = new UStringValueConverter ();
  481. var listWrapperConverter = new ListWrapperConverter ();
  482. var lstItems = new Binding (this, "Items", _lstItems, "Source", listWrapperConverter);
  483. void SetFrameDetails (StatusItem statusItem = null)
  484. {
  485. StatusItem newStatusItem;
  486. if (statusItem == null)
  487. {
  488. newStatusItem = DataContext.Items.Count > 0
  489. ? DataContext.Items [_lstItems.SelectedItem].StatusItem
  490. : null;
  491. }
  492. else
  493. {
  494. newStatusItem = statusItem;
  495. }
  496. _currentEditStatusItem = newStatusItem;
  497. _frmStatusBarDetails.EditStatusItem (newStatusItem);
  498. bool f = _btnOk.Enabled == _frmStatusBarDetails.Enabled;
  499. if (!f)
  500. {
  501. _btnOk.Enabled = _frmStatusBarDetails.Enabled;
  502. _btnCancel.Enabled = _frmStatusBarDetails.Enabled;
  503. }
  504. }
  505. void SetListViewSource (StatusItem _currentStatusItem, bool fill = false)
  506. {
  507. DataContext.Items = new List<DynamicStatusItemList> ();
  508. StatusItem statusItem = _currentStatusItem;
  509. if (!fill)
  510. {
  511. return;
  512. }
  513. if (statusItem != null)
  514. {
  515. foreach (StatusItem si in _statusBar.Items)
  516. {
  517. DataContext.Items.Add (new DynamicStatusItemList (si.Title, si));
  518. }
  519. }
  520. }
  521. StatusItem CreateNewStatusBar (DynamicStatusItem item)
  522. {
  523. var newStatusItem = new StatusItem (
  524. ShortcutHelper.GetShortcutFromTag (
  525. item.Shortcut,
  526. StatusBar.ShortcutDelimiter
  527. ),
  528. item.Title,
  529. _frmStatusBarDetails.CreateAction (item)
  530. );
  531. return newStatusItem;
  532. }
  533. void UpdateStatusItem (
  534. StatusItem _currentEditStatusItem,
  535. DynamicStatusItem statusItem,
  536. int index
  537. )
  538. {
  539. _currentEditStatusItem = CreateNewStatusBar (statusItem);
  540. _statusBar.Items [index] = _currentEditStatusItem;
  541. if (DataContext.Items.Count == 0)
  542. {
  543. DataContext.Items.Add (
  544. new DynamicStatusItemList (
  545. _currentEditStatusItem.Title,
  546. _currentEditStatusItem
  547. )
  548. );
  549. }
  550. DataContext.Items [index] = new DynamicStatusItemList (
  551. _currentEditStatusItem.Title,
  552. _currentEditStatusItem
  553. );
  554. SetFrameDetails (_currentEditStatusItem);
  555. }
  556. //_frmStatusBarDetails.Initialized += (s, e) => _frmStatusBarDetails.Enabled = false;
  557. }
  558. public DynamicStatusItemModel DataContext { get; set; }
  559. public static string SetTitleText (string title, string shortcut)
  560. {
  561. string txt = title;
  562. string [] split = title.Split ('~');
  563. if (split.Length > 1)
  564. {
  565. txt = split [2].Trim ();
  566. ;
  567. }
  568. if (string.IsNullOrEmpty (shortcut))
  569. {
  570. return txt;
  571. }
  572. return $"~{shortcut}~ {txt}";
  573. }
  574. }
  575. public class DynamicStatusItem
  576. {
  577. public string Action { get; set; } = "";
  578. public string Shortcut { get; set; }
  579. public string Title { get; set; } = "New";
  580. }
  581. public class DynamicStatusItemList
  582. {
  583. public DynamicStatusItemList () { }
  584. public DynamicStatusItemList (string title, StatusItem statusItem)
  585. {
  586. Title = title;
  587. StatusItem = statusItem;
  588. }
  589. public StatusItem StatusItem { get; set; }
  590. public string Title { get; set; }
  591. public override string ToString () { return $"{Title}, {StatusItem}"; }
  592. }
  593. public class DynamicStatusItemModel : INotifyPropertyChanged
  594. {
  595. private List<DynamicStatusItemList> _items;
  596. private string _statusBar;
  597. public DynamicStatusItemModel () { Items = []; }
  598. public List<DynamicStatusItemList> Items
  599. {
  600. get => _items;
  601. set
  602. {
  603. if (value == _items)
  604. {
  605. return;
  606. }
  607. _items = value;
  608. PropertyChanged?.Invoke (
  609. this,
  610. new PropertyChangedEventArgs (GetPropertyName ())
  611. );
  612. }
  613. }
  614. public string StatusBar
  615. {
  616. get => _statusBar;
  617. set
  618. {
  619. if (value == _statusBar)
  620. {
  621. return;
  622. }
  623. _statusBar = value;
  624. PropertyChanged?.Invoke (
  625. this,
  626. new PropertyChangedEventArgs (GetPropertyName ())
  627. );
  628. }
  629. }
  630. public event PropertyChangedEventHandler PropertyChanged;
  631. public string GetPropertyName ([CallerMemberName] string propertyName = null) { return propertyName; }
  632. }
  633. public interface IValueConverter
  634. {
  635. object Convert (object value, object parameter = null);
  636. }
  637. public class ListWrapperConverter : IValueConverter
  638. {
  639. public object Convert (object value, object parameter = null) { return new ListWrapper ((IList)value); }
  640. }
  641. public class UStringValueConverter : IValueConverter
  642. {
  643. public object Convert (object value, object parameter = null)
  644. {
  645. byte [] data = Encoding.ASCII.GetBytes (value.ToString () ?? string.Empty);
  646. return StringExtensions.ToString (data);
  647. }
  648. }
  649. }