ComboBoxIteration.cs 2.0 KB

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