ComboBoxIteration.cs 3.0 KB

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