ComboBoxIteration.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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.ColorSchemes ["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. HideDropdownListOnClick = true
  33. };
  34. comboBox.SetSource (items);
  35. listview.SelectedItemChanged += (s,e) => {
  36. lbListView.Text = items [e.Item];
  37. comboBox.SelectedItem = e.Item;
  38. };
  39. comboBox.SelectedItemChanged += (object sender, ListViewItemEventArgs text) => {
  40. if (text.Item != -1) {
  41. lbComboBox.Text = text.Value.ToString ();
  42. listview.SelectedItem = text.Item;
  43. }
  44. };
  45. Win.Add (lbComboBox, comboBox);
  46. Win.Add (new TextField { X = Pos.Right (listview) + 1, Y = Pos.Top (comboBox) + 3, Height = 1, Width = 20 });
  47. var btnTwo = new Button ("Two") {
  48. X = Pos.Right (comboBox) + 1,
  49. };
  50. btnTwo.Clicked += (s,e) => {
  51. items = new List<string> () { "one", "two" };
  52. comboBox.SetSource (items);
  53. listview.SetSource (items);
  54. listview.SelectedItem = 0;
  55. };
  56. Win.Add (btnTwo);
  57. var btnThree = new Button ("Three") {
  58. X = Pos.Right (comboBox) + 1,
  59. Y = Pos.Top (comboBox)
  60. };
  61. btnThree.Clicked += (s,e) => {
  62. items = new List<string> () { "one", "two", "three" };
  63. comboBox.SetSource (items);
  64. listview.SetSource (items);
  65. listview.SelectedItem = 0;
  66. };
  67. Win.Add (btnThree);
  68. }
  69. }
  70. }