DynamicStatusBar.cs 30 KB

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