Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ScrollToOnFocusBehaviour.cs
1using System.ComponentModel;
2using System.Diagnostics;
5
7{
11 public class ScrollToOnFocusBehavior : Behavior<VisualElement>
12 {
16 [TypeConverter(typeof(ReferenceTypeConverter))]
17 public ScrollView? TargetScrollView { get; set; }
18
22 [TypeConverter(typeof(ReferenceTypeConverter))]
23 public VisualElement? TargetElement { get; set; }
24
29
33 public static readonly BindableProperty IsEnabledProperty =
34 BindableProperty.Create(nameof(IsEnabled), typeof(bool), typeof(ScrollToClickedBehavior), true);
35
39 public bool IsEnabled
40 {
41 get => (bool)this.GetValue(IsEnabledProperty);
42 set => this.SetValue(IsEnabledProperty, value);
43 }
44
45 protected override void OnAttachedTo(VisualElement bindable)
46 {
47
48 base.OnAttachedTo(bindable);
49
50 if(bindable is CompositeEntry entry)
51 entry.Entry.Focused += this.OnElementFocused;
52 else
53 bindable.Focused += this.OnElementFocused;
54 }
55
56 protected override void OnDetachingFrom(VisualElement bindable)
57 {
58 base.OnDetachingFrom(bindable);
59 if(bindable is CompositeEntry entry)
60 entry.Entry.Focused -= this.OnElementFocused;
61 else
62 bindable.Focused -= this.OnElementFocused;
63 }
64
65 private async void OnElementFocused(object? sender, FocusEventArgs e)
66 {
67 if(sender is null or not VisualElement)
68 {
69 return;
70 }
71 if(sender is CompositeEntry or Entry)
72 await Task.Delay(200); //allow keyboard to show if there is any
73
74 VisualElement? target = this.TargetElement ?? (VisualElement)sender;
75
76 if (this.TargetScrollView is not null)
77 {
78 await this.TargetScrollView.ScrollToAsync(target, ScrollToPosition.Start, true);
79 }
80 else
81 {
82 Element Loop = ((VisualElement)sender).Parent;
83
84 while (Loop is not null && Loop is not ScrollView)
85 Loop = Loop.Parent;
86
87 (Loop as ScrollView)?.ScrollToAsync(target, this.ScrollToPosition, true);
88 }
89 }
90 }
91}
Used for moving focus to the next UI component when a button has been clicked.
Used for moving focus to the next UI component when a button has been clicked.
bool IsEnabled
Gets or sets a value indicating if behavior is enabled or disabled
ScrollView? TargetScrollView
The ScrollView to scroll. If null, the first parent ScrollView will be used.
ScrollToPosition ScrollToPosition
The scroll behavior to use when scrolling to the element.
static readonly BindableProperty IsEnabledProperty
A BindableProperty for IsEnabled property.
VisualElement? TargetElement
The element to scroll to. If null, the element that has focus will be scrolled to.