DynamicStatusBar.cs 19 KB

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