ItemsRepeaterPage.xaml.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 Avalonia.VisualTree;
  9. using ControlCatalog.ViewModels;
  10. namespace ControlCatalog.Pages
  11. {
  12. public class ItemsRepeaterPage : UserControl
  13. {
  14. private readonly ItemsRepeaterPageViewModel _viewModel;
  15. private ItemsRepeater _repeater;
  16. private ScrollViewer _scroller;
  17. private int _selectedIndex;
  18. private Button _scrollToLast;
  19. private Button _scrollToRandom;
  20. private Button _scrollToSelected;
  21. private Random _random = new Random(0);
  22. public ItemsRepeaterPage()
  23. {
  24. this.InitializeComponent();
  25. _repeater = this.Get<ItemsRepeater>("repeater");
  26. _scroller = this.Get<ScrollViewer>("scroller");
  27. _scrollToLast = this.Get<Button>("scrollToLast");
  28. _scrollToRandom = this.Get<Button>("scrollToRandom");
  29. _scrollToSelected = this.Get<Button>("scrollToSelected");
  30. _repeater.PointerPressed += RepeaterClick;
  31. _repeater.KeyDown += RepeaterOnKeyDown;
  32. _scrollToLast.Click += scrollToLast_Click;
  33. _scrollToRandom.Click += scrollToRandom_Click;
  34. _scrollToSelected.Click += scrollToSelected_Click;
  35. DataContext = _viewModel = new ItemsRepeaterPageViewModel();
  36. }
  37. private void InitializeComponent()
  38. {
  39. AvaloniaXamlLoader.Load(this);
  40. }
  41. public void OnSelectTemplateKey(object sender, SelectTemplateEventArgs e)
  42. {
  43. if (e.DataContext is ItemsRepeaterPageViewModel.Item item)
  44. {
  45. e.TemplateKey = (item.Index % 2 == 0) ? "even" : "odd";
  46. }
  47. }
  48. private void LayoutChanged(object sender, SelectionChangedEventArgs e)
  49. {
  50. if (_repeater == null)
  51. {
  52. return;
  53. }
  54. var comboBox = (ComboBox)sender;
  55. switch (comboBox.SelectedIndex)
  56. {
  57. case 0:
  58. _scroller.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto;
  59. _scroller.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
  60. _repeater.Layout = new StackLayout { Orientation = Orientation.Vertical };
  61. break;
  62. case 1:
  63. _scroller.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto;
  64. _scroller.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
  65. _repeater.Layout = new StackLayout { Orientation = Orientation.Horizontal };
  66. break;
  67. case 2:
  68. _scroller.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto;
  69. _scroller.VerticalScrollBarVisibility = ScrollBarVisibility.Disabled;
  70. _repeater.Layout = new UniformGridLayout
  71. {
  72. Orientation = Orientation.Vertical,
  73. MinItemWidth = 200,
  74. MinItemHeight = 200,
  75. };
  76. break;
  77. case 3:
  78. _scroller.HorizontalScrollBarVisibility = ScrollBarVisibility.Disabled;
  79. _scroller.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
  80. _repeater.Layout = new UniformGridLayout
  81. {
  82. Orientation = Orientation.Horizontal,
  83. MinItemWidth = 200,
  84. MinItemHeight = 200,
  85. };
  86. break;
  87. case 4:
  88. _scroller.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto;
  89. _scroller.VerticalScrollBarVisibility = ScrollBarVisibility.Disabled;
  90. _repeater.Layout = new WrapLayout
  91. {
  92. Orientation = Orientation.Vertical,
  93. HorizontalSpacing = 20,
  94. VerticalSpacing = 20
  95. };
  96. break;
  97. case 5:
  98. _scroller.HorizontalScrollBarVisibility = ScrollBarVisibility.Disabled;
  99. _scroller.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
  100. _repeater.Layout = new WrapLayout
  101. {
  102. Orientation = Orientation.Horizontal,
  103. HorizontalSpacing = 20,
  104. VerticalSpacing = 20
  105. };
  106. break;
  107. }
  108. }
  109. private void ScrollTo(int index)
  110. {
  111. System.Diagnostics.Debug.WriteLine("Scroll to " + index);
  112. var layoutManager = ((TopLevel)VisualRoot!).LayoutManager;
  113. var element = _repeater.GetOrCreateElement(index);
  114. layoutManager.ExecuteLayoutPass();
  115. element.BringIntoView();
  116. }
  117. private void RepeaterClick(object sender, PointerPressedEventArgs e)
  118. {
  119. if ((e.Source as TextBlock)?.DataContext is ItemsRepeaterPageViewModel.Item item)
  120. {
  121. _viewModel.SelectedItem = item;
  122. _selectedIndex = _viewModel.Items.IndexOf(item);
  123. }
  124. }
  125. private void RepeaterOnKeyDown(object sender, KeyEventArgs e)
  126. {
  127. if (e.Key == Key.F5)
  128. {
  129. _viewModel.ResetItems();
  130. }
  131. }
  132. private void scrollToLast_Click(object sender, Avalonia.Interactivity.RoutedEventArgs e)
  133. {
  134. ScrollTo(_viewModel.Items.Count - 1);
  135. }
  136. private void scrollToRandom_Click(object sender, Avalonia.Interactivity.RoutedEventArgs e)
  137. {
  138. ScrollTo(_random.Next(_viewModel.Items.Count - 1));
  139. }
  140. private void scrollToSelected_Click(object sender, Avalonia.Interactivity.RoutedEventArgs e)
  141. {
  142. ScrollTo(_selectedIndex);
  143. }
  144. }
  145. }