DynamicStatusBar.cs 19 KB

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