ComboBoxIteration.cs 3.1 KB

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