ListsAndCombos.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using System;
  2. using System.Linq;
  3. using System.IO;
  4. using System.Collections.Generic;
  5. using Terminal.Gui;
  6. using NStack;
  7. namespace UICatalog.Scenarios {
  8. [ScenarioMetadata (Name: "ListView & ComboBox", Description: "Demonstrates a ListView populating a ComboBox that acts as a filter.")]
  9. [ScenarioCategory ("Controls")]
  10. class ListsAndCombos : Scenario {
  11. public override void Setup ()
  12. {
  13. //TODO: Duplicated code in Demo.cs Consider moving to shared assembly
  14. var items = new List<ustring> ();
  15. foreach (var dir in new [] { "/etc", @$"{Environment.GetEnvironmentVariable ("SystemRoot")}\System32" }) {
  16. if (Directory.Exists (dir)) {
  17. items = Directory.GetFiles (dir).Union(Directory.GetDirectories(dir))
  18. .Select (Path.GetFileName)
  19. .Where (x => char.IsLetterOrDigit (x [0]))
  20. .OrderBy (x => x).Select(x => ustring.Make(x)).ToList() ;
  21. }
  22. }
  23. // ListView
  24. var lbListView = new Label ("Listview") {
  25. ColorScheme = Colors.TopLevel,
  26. X = 0,
  27. Width = Dim.Percent (40)
  28. };
  29. var listview = new ListView (items) {
  30. X = 0,
  31. Y = Pos.Bottom (lbListView) + 1,
  32. Height = Dim.Fill(2),
  33. Width = Dim.Percent (40)
  34. };
  35. listview.SelectedItemChanged += (ListViewItemEventArgs e) => lbListView.Text = items [listview.SelectedItem];
  36. Win.Add (lbListView, listview);
  37. // ComboBox
  38. var lbComboBox = new Label ("ComboBox") {
  39. ColorScheme = Colors.TopLevel,
  40. X = Pos.Right (lbListView) + 1,
  41. Width = Dim.Percent(40)
  42. };
  43. var comboBox = new ComboBox () {
  44. X = Pos.Right (listview) + 1,
  45. Y = Pos.Bottom (lbListView) + 1,
  46. Height = Dim.Fill (2),
  47. Width = Dim.Percent(40)
  48. };
  49. comboBox.SetSource (items);
  50. comboBox.SelectedItemChanged += (ListViewItemEventArgs text) => lbComboBox.Text = items[comboBox.SelectedItem];
  51. Win.Add (lbComboBox, comboBox);
  52. }
  53. }
  54. }