ComboBoxIteration.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 Setup ()
  11. {
  12. ObservableCollection<string> items = ["one", "two", "three"];
  13. var lbListView = new Label { Width = 10, Height = 1 };
  14. Win.Add (lbListView);
  15. var listview = new ListView
  16. {
  17. Y = Pos.Bottom (lbListView) + 1, Width = 10, Height = Dim.Fill (2), Source = new ListWrapper<string> (items)
  18. };
  19. Win.Add (listview);
  20. var lbComboBox = new Label
  21. {
  22. ColorScheme = Colors.ColorSchemes ["TopLevel"],
  23. X = Pos.Right (lbListView) + 1,
  24. Width = Dim.Percent (40)
  25. };
  26. var comboBox = new ComboBox
  27. {
  28. X = Pos.Right (listview) + 1,
  29. Y = Pos.Bottom (lbListView) + 1,
  30. Height = Dim.Fill (2),
  31. Width = Dim.Percent (40),
  32. HideDropdownListOnClick = true
  33. };
  34. comboBox.SetSource (items);
  35. listview.SelectedItemChanged += (s, e) =>
  36. {
  37. lbListView.Text = items [e.Item];
  38. comboBox.SelectedItem = e.Item;
  39. };
  40. comboBox.SelectedItemChanged += (sender, text) =>
  41. {
  42. if (text.Item != -1)
  43. {
  44. lbComboBox.Text = text.Value.ToString ();
  45. listview.SelectedItem = text.Item;
  46. }
  47. };
  48. Win.Add (lbComboBox, comboBox);
  49. Win.Add (new TextField { X = Pos.Right (listview) + 1, Y = Pos.Top (comboBox) + 3, Height = 1, Width = 20 });
  50. var btnTwo = new Button { X = Pos.Right (comboBox) + 1, Text = "Two" };
  51. btnTwo.Accept += (s, e) =>
  52. {
  53. items = ["one", "two"];
  54. comboBox.SetSource (items);
  55. listview.SetSource (items);
  56. listview.SelectedItem = 0;
  57. };
  58. Win.Add (btnTwo);
  59. var btnThree = new Button { X = Pos.Right (comboBox) + 1, Y = Pos.Top (comboBox), Text = "Three" };
  60. btnThree.Accept += (s, e) =>
  61. {
  62. items =["one", "two", "three"];
  63. comboBox.SetSource (items);
  64. listview.SetSource (items);
  65. listview.SelectedItem = 0;
  66. };
  67. Win.Add (btnThree);
  68. }
  69. }