DynamicStatusBar.cs 18 KB

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