ComboBoxIteration.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using System.Collections.Generic;
  2. using System.Collections.ObjectModel;
  3. using Terminal.Gui;
  4. namespace UICatalog.Scenarios;
  5. [ScenarioMetadata ("ComboBoxIteration", "ComboBox iteration.")]
  6. [ScenarioCategory ("Controls")]
  7. [ScenarioCategory ("ComboBox")]
  8. public class ComboBoxIteration : Scenario
  9. {
  10. public override void Main ()
  11. {
  12. Application.Init ();
  13. ObservableCollection<string> items = ["one", "two", "three"];
  14. var win = new Window { Title = GetQuitKeyAndName () };
  15. var lbListView = new Label { Width = 10, Height = 1 };
  16. win.Add (lbListView);
  17. var listview = new ListView
  18. {
  19. Y = Pos.Bottom (lbListView) + 1, Width = 10, Height = Dim.Fill (2), Source = new ListWrapper<string> (items)
  20. };
  21. win.Add (listview);
  22. var lbComboBox = new Label
  23. {
  24. ColorScheme = Colors.ColorSchemes ["TopLevel"],
  25. X = Pos.Right (lbListView) + 1,
  26. Width = Dim.Percent (40)
  27. };
  28. var comboBox = new ComboBox
  29. {
  30. X = Pos.Right (listview) + 1,
  31. Y = Pos.Bottom (lbListView) + 1,
  32. Height = Dim.Fill (2),
  33. Width = Dim.Percent (40),
  34. HideDropdownListOnClick = true
  35. };
  36. comboBox.SetSource (items);
  37. listview.SelectedItemChanged += (s, e) =>
  38. {
  39. lbListView.Text = items [e.Item];
  40. comboBox.SelectedItem = e.Item;
  41. };
  42. comboBox.SelectedItemChanged += (sender, text) =>
  43. {
  44. if (text.Item != -1)
  45. {
  46. lbComboBox.Text = text.Value.ToString ();
  47. listview.SelectedItem = text.Item;
  48. }
  49. };
  50. win.Add (lbComboBox, comboBox);
  51. win.Add (new TextField { X = Pos.Right (listview) + 1, Y = Pos.Top (comboBox) + 3, Height = 1, Width = 20 });
  52. var btnTwo = new Button { X = Pos.Right (comboBox) + 1, Text = "Two" };
  53. btnTwo.Accepted += (s, e) =>
  54. {
  55. items = ["one", "two"];
  56. comboBox.SetSource (items);
  57. listview.SetSource (items);
  58. listview.SelectedItem = 0;
  59. };
  60. win.Add (btnTwo);
  61. var btnThree = new Button { X = Pos.Right (comboBox) + 1, Y = Pos.Top (comboBox), Text = "Three" };
  62. btnThree.Accepted += (s, e) =>
  63. {
  64. items =["one", "two", "three"];
  65. comboBox.SetSource (items);
  66. listview.SetSource (items);
  67. listview.SelectedItem = 0;
  68. };
  69. win.Add (btnThree);
  70. Application.Run (win);
  71. win.Dispose ();
  72. Application.Shutdown ();
  73. }
  74. }