ListsAndCombos.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. using System;
  2. using System.Collections.ObjectModel;
  3. using System.IO;
  4. using System.Linq;
  5. namespace UICatalog.Scenarios;
  6. [ScenarioMetadata ("ListView & ComboBox", "Demonstrates a ListView populating a ComboBox that acts as a filter.")]
  7. [ScenarioCategory ("Controls")]
  8. [ScenarioCategory ("ListView")]
  9. [ScenarioCategory ("ComboBox")]
  10. public class ListsAndCombos : Scenario
  11. {
  12. public override void Main ()
  13. {
  14. Application.Init ();
  15. //TODO: Duplicated code in Demo.cs Consider moving to shared assembly
  16. ObservableCollection<string> items = [];
  17. foreach (string dir in new [] { "/etc", @$"{Environment.GetEnvironmentVariable ("SystemRoot")}\System32" })
  18. {
  19. if (Directory.Exists (dir))
  20. {
  21. items = new (Directory.GetFiles (dir)
  22. .Union (Directory.GetDirectories (dir))
  23. .Select (Path.GetFileName)
  24. .Where (x => char.IsLetterOrDigit (x [0]))
  25. .OrderBy (x => x)
  26. .Select (x => x)
  27. .ToList ());
  28. }
  29. }
  30. var win = new Window { Title = GetQuitKeyAndName () };
  31. // ListView
  32. var lbListView = new Label
  33. {
  34. SchemeName = "Runnable",
  35. X = 0,
  36. Width = Dim.Percent (40),
  37. Text = "Listview"
  38. };
  39. var listview = new ListView
  40. {
  41. X = 0,
  42. Y = Pos.Bottom (lbListView) + 2,
  43. Height = Dim.Fill (2),
  44. Width = Dim.Percent (40),
  45. Source = new ListWrapper<string> (items)
  46. };
  47. listview.SelectedItemChanged += (s, e) => lbListView.Text = items [listview.SelectedItem.Value];
  48. win.Add (lbListView, listview);
  49. //var scrollBar = new ScrollBarView (listview, true);
  50. //scrollBar.ChangedPosition += (s, e) =>
  51. // {
  52. // listview.TopItem = scrollBar.Position;
  53. // if (listview.TopItem != scrollBar.Position)
  54. // {
  55. // scrollBar.Position = listview.TopItem;
  56. // }
  57. // listview.SetNeedsDraw ();
  58. // };
  59. //scrollBar.OtherScrollBarView.ChangedPosition += (s, e) =>
  60. // {
  61. // listview.LeftItem = scrollBar.OtherScrollBarView.Position;
  62. // if (listview.LeftItem != scrollBar.OtherScrollBarView.Position)
  63. // {
  64. // scrollBar.OtherScrollBarView.Position = listview.LeftItem;
  65. // }
  66. // listview.SetNeedsDraw ();
  67. // };
  68. //listview.DrawingContent += (s, e) =>
  69. // {
  70. // scrollBar.Size = listview.Source.Count - 1;
  71. // scrollBar.Position = listview.TopItem;
  72. // scrollBar.OtherScrollBarView.Size = listview.MaxLength - 1;
  73. // scrollBar.OtherScrollBarView.Position = listview.LeftItem;
  74. // scrollBar.Refresh ();
  75. // };
  76. // ComboBox
  77. var lbComboBox = new Label
  78. {
  79. SchemeName = "Runnable",
  80. X = Pos.Right (lbListView) + 1,
  81. Width = Dim.Percent (40),
  82. Text = "ComboBox"
  83. };
  84. var comboBox = new ComboBox
  85. {
  86. X = Pos.Right (listview) + 1,
  87. Y = Pos.Bottom (lbListView) + 1,
  88. Height = Dim.Fill (2),
  89. Width = Dim.Percent (40)
  90. };
  91. comboBox.SetSource (items);
  92. comboBox.SelectedItemChanged += (s, text) => lbComboBox.Text = text.Value.ToString ();
  93. win.Add (lbComboBox, comboBox);
  94. //var scrollBarCbx = new ScrollBarView (comboBox.SubViews.ElementAt (1), true);
  95. //scrollBarCbx.ChangedPosition += (s, e) =>
  96. // {
  97. // ((ListView)comboBox.SubViews.ElementAt (1)).TopItem = scrollBarCbx.Position;
  98. // if (((ListView)comboBox.SubViews.ElementAt (1)).TopItem != scrollBarCbx.Position)
  99. // {
  100. // scrollBarCbx.Position = ((ListView)comboBox.SubViews.ElementAt (1)).TopItem;
  101. // }
  102. // comboBox.SetNeedsDraw ();
  103. // };
  104. //scrollBarCbx.OtherScrollBarView.ChangedPosition += (s, e) =>
  105. // {
  106. // ((ListView)comboBox.SubViews.ElementAt (1)).LeftItem = scrollBarCbx.OtherScrollBarView.Position;
  107. // if (((ListView)comboBox.SubViews.ElementAt (1)).LeftItem != scrollBarCbx.OtherScrollBarView.Position)
  108. // {
  109. // scrollBarCbx.OtherScrollBarView.Position = ((ListView)comboBox.SubViews.ElementAt (1)).LeftItem;
  110. // }
  111. // comboBox.SetNeedsDraw ();
  112. // };
  113. //comboBox.DrawingContent += (s, e) =>
  114. // {
  115. // scrollBarCbx.Size = comboBox.Source.Count;
  116. // scrollBarCbx.Position = ((ListView)comboBox.SubViews.ElementAt (1)).TopItem;
  117. // scrollBarCbx.OtherScrollBarView.Size = ((ListView)comboBox.SubViews.ElementAt (1)).MaxLength - 1;
  118. // scrollBarCbx.OtherScrollBarView.Position = ((ListView)comboBox.SubViews.ElementAt (1)).LeftItem;
  119. // scrollBarCbx.Refresh ();
  120. // };
  121. var btnMoveUp = new Button { X = 1, Y = Pos.Bottom (lbListView), Text = "Move _Up" };
  122. btnMoveUp.Accepting += (s, e) => { listview.MoveUp (); };
  123. var btnMoveDown = new Button
  124. {
  125. X = Pos.Right (btnMoveUp) + 1, Y = Pos.Bottom (lbListView), Text = "Move _Down"
  126. };
  127. btnMoveDown.Accepting += (s, e) => { listview.MoveDown (); };
  128. win.Add (btnMoveUp, btnMoveDown);
  129. Application.Run (win);
  130. win.Dispose ();
  131. Application.Shutdown ();
  132. }
  133. }