ItemsRepeaterPage.xaml.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using System;
  2. using System.Linq;
  3. using Avalonia.Controls;
  4. using Avalonia.Controls.Primitives;
  5. using Avalonia.Input;
  6. using Avalonia.Layout;
  7. using Avalonia.Markup.Xaml;
  8. using ControlCatalog.ViewModels;
  9. namespace ControlCatalog.Pages
  10. {
  11. public class ItemsRepeaterPage : UserControl
  12. {
  13. private ItemsRepeater _repeater;
  14. private ScrollViewer _scroller;
  15. public ItemsRepeaterPage()
  16. {
  17. this.InitializeComponent();
  18. _repeater = this.FindControl<ItemsRepeater>("repeater");
  19. _scroller = this.FindControl<ScrollViewer>("scroller");
  20. _repeater.PointerPressed += RepeaterClick;
  21. _repeater.KeyDown += RepeaterOnKeyDown;
  22. DataContext = new ItemsRepeaterPageViewModel();
  23. }
  24. private void InitializeComponent()
  25. {
  26. AvaloniaXamlLoader.Load(this);
  27. }
  28. private void LayoutChanged(object sender, SelectionChangedEventArgs e)
  29. {
  30. if (_repeater == null)
  31. {
  32. return;
  33. }
  34. var comboBox = (ComboBox)sender;
  35. switch (comboBox.SelectedIndex)
  36. {
  37. case 0:
  38. _scroller.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto;
  39. _scroller.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
  40. _repeater.Layout = new StackLayout { Orientation = Orientation.Vertical };
  41. break;
  42. case 1:
  43. _scroller.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto;
  44. _scroller.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
  45. _repeater.Layout = new StackLayout { Orientation = Orientation.Horizontal };
  46. break;
  47. case 2:
  48. _scroller.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto;
  49. _scroller.VerticalScrollBarVisibility = ScrollBarVisibility.Disabled;
  50. _repeater.Layout = new UniformGridLayout
  51. {
  52. Orientation = Orientation.Vertical,
  53. MinItemWidth = 200,
  54. MinItemHeight = 200,
  55. };
  56. break;
  57. case 3:
  58. _scroller.HorizontalScrollBarVisibility = ScrollBarVisibility.Disabled;
  59. _scroller.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
  60. _repeater.Layout = new UniformGridLayout
  61. {
  62. Orientation = Orientation.Horizontal,
  63. MinItemWidth = 200,
  64. MinItemHeight = 200,
  65. };
  66. break;
  67. }
  68. }
  69. private void RepeaterClick(object sender, PointerPressedEventArgs e)
  70. {
  71. var item = (e.Source as TextBlock)?.DataContext as ItemsRepeaterPageViewModel.Item;
  72. ((ItemsRepeaterPageViewModel)DataContext).SelectedItem = item;
  73. }
  74. private void RepeaterOnKeyDown(object sender, KeyEventArgs e)
  75. {
  76. if (e.Key == Key.F5)
  77. {
  78. ((ItemsRepeaterPageViewModel)DataContext).ResetItems();
  79. }
  80. }
  81. }
  82. }