ItemsRepeaterPage.xaml.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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.FindControl<ItemsRepeater>("repeater");
  26. _scroller = this.FindControl<ScrollViewer>("scroller");
  27. _scrollToLast = this.FindControl<Button>("scrollToLast");
  28. _scrollToRandom = this.FindControl<Button>("scrollToRandom");
  29. _scrollToSelected = this.FindControl<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. var item = (ItemsRepeaterPageViewModel.Item)e.DataContext;
  44. e.TemplateKey = (item.Index % 2 == 0) ? "even" : "odd";
  45. }
  46. private void LayoutChanged(object sender, SelectionChangedEventArgs e)
  47. {
  48. if (_repeater == null)
  49. {
  50. return;
  51. }
  52. var comboBox = (ComboBox)sender;
  53. switch (comboBox.SelectedIndex)
  54. {
  55. case 0:
  56. _scroller.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto;
  57. _scroller.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
  58. _repeater.Layout = new StackLayout { Orientation = Orientation.Vertical };
  59. break;
  60. case 1:
  61. _scroller.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto;
  62. _scroller.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
  63. _repeater.Layout = new StackLayout { Orientation = Orientation.Horizontal };
  64. break;
  65. case 2:
  66. _scroller.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto;
  67. _scroller.VerticalScrollBarVisibility = ScrollBarVisibility.Disabled;
  68. _repeater.Layout = new UniformGridLayout
  69. {
  70. Orientation = Orientation.Vertical,
  71. MinItemWidth = 200,
  72. MinItemHeight = 200,
  73. };
  74. break;
  75. case 3:
  76. _scroller.HorizontalScrollBarVisibility = ScrollBarVisibility.Disabled;
  77. _scroller.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
  78. _repeater.Layout = new UniformGridLayout
  79. {
  80. Orientation = Orientation.Horizontal,
  81. MinItemWidth = 200,
  82. MinItemHeight = 200,
  83. };
  84. break;
  85. case 4:
  86. _scroller.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto;
  87. _scroller.VerticalScrollBarVisibility = ScrollBarVisibility.Disabled;
  88. _repeater.Layout = new WrapLayout
  89. {
  90. Orientation = Orientation.Vertical,
  91. HorizontalSpacing = 20,
  92. VerticalSpacing = 20
  93. };
  94. break;
  95. case 5:
  96. _scroller.HorizontalScrollBarVisibility = ScrollBarVisibility.Disabled;
  97. _scroller.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
  98. _repeater.Layout = new WrapLayout
  99. {
  100. Orientation = Orientation.Horizontal,
  101. HorizontalSpacing = 20,
  102. VerticalSpacing = 20
  103. };
  104. break;
  105. }
  106. }
  107. private void ScrollTo(int index)
  108. {
  109. System.Diagnostics.Debug.WriteLine("Scroll to " + index);
  110. var layoutManager = ((TopLevel)VisualRoot).LayoutManager;
  111. var element = _repeater.GetOrCreateElement(index);
  112. layoutManager.ExecuteLayoutPass();
  113. element.BringIntoView();
  114. }
  115. private void RepeaterClick(object sender, PointerPressedEventArgs e)
  116. {
  117. var item = (e.Source as TextBlock)?.DataContext as ItemsRepeaterPageViewModel.Item;
  118. _viewModel.SelectedItem = item;
  119. _selectedIndex = _viewModel.Items.IndexOf(item);
  120. }
  121. private void RepeaterOnKeyDown(object sender, KeyEventArgs e)
  122. {
  123. if (e.Key == Key.F5)
  124. {
  125. _viewModel.ResetItems();
  126. }
  127. }
  128. private void scrollToLast_Click(object sender, Avalonia.Interactivity.RoutedEventArgs e)
  129. {
  130. ScrollTo(_viewModel.Items.Count - 1);
  131. }
  132. private void scrollToRandom_Click(object sender, Avalonia.Interactivity.RoutedEventArgs e)
  133. {
  134. ScrollTo(_random.Next(_viewModel.Items.Count - 1));
  135. }
  136. private void scrollToSelected_Click(object sender, Avalonia.Interactivity.RoutedEventArgs e)
  137. {
  138. ScrollTo(_selectedIndex);
  139. }
  140. }
  141. }