DynamicStatusBar.cs 19 KB

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