|
@@ -5,23 +5,32 @@ using Avalonia.Controls.Primitives;
|
|
|
using Avalonia.Input;
|
|
using Avalonia.Input;
|
|
|
using Avalonia.Layout;
|
|
using Avalonia.Layout;
|
|
|
using Avalonia.Markup.Xaml;
|
|
using Avalonia.Markup.Xaml;
|
|
|
|
|
+using Avalonia.VisualTree;
|
|
|
using ControlCatalog.ViewModels;
|
|
using ControlCatalog.ViewModels;
|
|
|
|
|
|
|
|
namespace ControlCatalog.Pages
|
|
namespace ControlCatalog.Pages
|
|
|
{
|
|
{
|
|
|
public class ItemsRepeaterPage : UserControl
|
|
public class ItemsRepeaterPage : UserControl
|
|
|
{
|
|
{
|
|
|
|
|
+ private readonly ItemsRepeaterPageViewModel _viewModel;
|
|
|
private ItemsRepeater _repeater;
|
|
private ItemsRepeater _repeater;
|
|
|
private ScrollViewer _scroller;
|
|
private ScrollViewer _scroller;
|
|
|
|
|
+ private Button _scrollToLast;
|
|
|
|
|
+ private Button _scrollToRandom;
|
|
|
|
|
+ private Random _random = new Random(0);
|
|
|
|
|
|
|
|
public ItemsRepeaterPage()
|
|
public ItemsRepeaterPage()
|
|
|
{
|
|
{
|
|
|
this.InitializeComponent();
|
|
this.InitializeComponent();
|
|
|
_repeater = this.FindControl<ItemsRepeater>("repeater");
|
|
_repeater = this.FindControl<ItemsRepeater>("repeater");
|
|
|
_scroller = this.FindControl<ScrollViewer>("scroller");
|
|
_scroller = this.FindControl<ScrollViewer>("scroller");
|
|
|
|
|
+ _scrollToLast = this.FindControl<Button>("scrollToLast");
|
|
|
|
|
+ _scrollToRandom = this.FindControl<Button>("scrollToRandom");
|
|
|
_repeater.PointerPressed += RepeaterClick;
|
|
_repeater.PointerPressed += RepeaterClick;
|
|
|
_repeater.KeyDown += RepeaterOnKeyDown;
|
|
_repeater.KeyDown += RepeaterOnKeyDown;
|
|
|
- DataContext = new ItemsRepeaterPageViewModel();
|
|
|
|
|
|
|
+ _scrollToLast.Click += scrollToLast_Click;
|
|
|
|
|
+ _scrollToRandom.Click += scrollToRandom_Click;
|
|
|
|
|
+ DataContext = _viewModel = new ItemsRepeaterPageViewModel();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private void InitializeComponent()
|
|
private void InitializeComponent()
|
|
@@ -73,18 +82,36 @@ namespace ControlCatalog.Pages
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ private void ScrollTo(int index)
|
|
|
|
|
+ {
|
|
|
|
|
+ var layoutManager = ((Window)this.GetVisualRoot()).LayoutManager;
|
|
|
|
|
+ var element = _repeater.GetOrCreateElement(index);
|
|
|
|
|
+ layoutManager.ExecuteLayoutPass();
|
|
|
|
|
+ element.BringIntoView();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
private void RepeaterClick(object sender, PointerPressedEventArgs e)
|
|
private void RepeaterClick(object sender, PointerPressedEventArgs e)
|
|
|
{
|
|
{
|
|
|
var item = (e.Source as TextBlock)?.DataContext as ItemsRepeaterPageViewModel.Item;
|
|
var item = (e.Source as TextBlock)?.DataContext as ItemsRepeaterPageViewModel.Item;
|
|
|
- ((ItemsRepeaterPageViewModel)DataContext).SelectedItem = item;
|
|
|
|
|
|
|
+ _viewModel.SelectedItem = item;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private void RepeaterOnKeyDown(object sender, KeyEventArgs e)
|
|
private void RepeaterOnKeyDown(object sender, KeyEventArgs e)
|
|
|
{
|
|
{
|
|
|
if (e.Key == Key.F5)
|
|
if (e.Key == Key.F5)
|
|
|
{
|
|
{
|
|
|
- ((ItemsRepeaterPageViewModel)DataContext).ResetItems();
|
|
|
|
|
|
|
+ _viewModel.ResetItems();
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ private void scrollToLast_Click(object sender, Avalonia.Interactivity.RoutedEventArgs e)
|
|
|
|
|
+ {
|
|
|
|
|
+ ScrollTo(_viewModel.Items.Count - 1);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void scrollToRandom_Click(object sender, Avalonia.Interactivity.RoutedEventArgs e)
|
|
|
|
|
+ {
|
|
|
|
|
+ ScrollTo(_random.Next(_viewModel.Items.Count - 1));
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|