From c7f401200478c0a6414ce7d314c24096b7bfcab7 Mon Sep 17 00:00:00 2001 From: Stepan Pilipenko Date: Tue, 18 Nov 2025 21:49:37 +0300 Subject: [PATCH] focus to button --- MainPage.xaml.cs | 59 +++++++++++++++++++++++++++++++----------------- 1 file changed, 38 insertions(+), 21 deletions(-) diff --git a/MainPage.xaml.cs b/MainPage.xaml.cs index 0f406d2..f6f55ae 100644 --- a/MainPage.xaml.cs +++ b/MainPage.xaml.cs @@ -79,15 +79,15 @@ public partial class MainPage : ContentPage, INotifyPropertyChanged private void InitializeCollectionViews() { LeftPanel.ItemTemplate = PanelCollectionView.CreateItemTemplate(isLeftPanel: true, page: this); - RightPanel.ItemTemplate = PanelCollectionView.CreateItemTemplate(isLeftPanel: true, page: this); + RightPanel.ItemTemplate = PanelCollectionView.CreateItemTemplate(isLeftPanel: false, page: this); } - protected override void OnHandlerChanged() - { - base.OnHandlerChanged(); - this.Focus(); - } + //protected override void OnHandlerChanged() + //{ + // base.OnHandlerChanged(); + // this.Focus(); + //} public void HandleItemClick(FileSystemItem item, bool isLeftPanel) { @@ -150,13 +150,13 @@ public partial class MainPage : ContentPage, INotifyPropertyChanged } // Переключаем фокус с указанием направления - private void MoveFocus(bool moveForward = true) + private void MoveFocus(bool moveForward, bool isLeft) { - var allFocusable = GetFocusableElements(); + var allFocusable = GetFocusableElements(isLeft); var current = allFocusable.FirstOrDefault(x => x.IsFocused); var next = moveForward ? - GetNextFocusable(current, allFocusable) : - GetPreviousFocusable(current, allFocusable); + GetNextFocusable(current, allFocusable) : + GetPreviousFocusable(current, allFocusable); next?.Focus(); } @@ -185,13 +185,30 @@ public partial class MainPage : ContentPage, INotifyPropertyChanged return allElements[prevIndex]; } - private List GetFocusableElements() + private List GetFocusableElements(bool isLeft) { - return this.GetVisualTreeDescendants() - .OfType() - .Where(x => x.IsEnabled && x.IsVisible) + List? elements = null; + if (isLeft) + { + elements = LeftPanel.GetVisualTreeDescendants().OfType() + .Where(x => x.IsEnabled && x.IsVisible && x is Button) + .OrderBy(GetVisualTreeOrder) // Сортируем по порядку в визуальном дереве + .ToList(); + } + else + { + elements = RightPanel.GetVisualTreeDescendants().OfType() + .Where(x => x.IsEnabled && x.IsVisible && x is Button) .OrderBy(GetVisualTreeOrder) // Сортируем по порядку в визуальном дереве .ToList(); + } + + //foreach (var el in elements) + //{ + // System.Diagnostics.Debug.WriteLine($"!!! {el.ToString()}"); + //} + + return elements; } private int GetVisualTreeOrder(View view) @@ -214,19 +231,19 @@ public partial class MainPage : ContentPage, INotifyPropertyChanged _leftSelectedIndex = LeftItems.IndexOf(selectedItem); _isLeftPanelActive = true; - System.Diagnostics.Debug.WriteLine($"L !!! {_leftSelectedIndex} {_lastLeftSelectedIndex}"); + // System.Diagnostics.Debug.WriteLine($"L !!! {_leftSelectedIndex} {_lastLeftSelectedIndex}"); if (_lastLeftSelectedIndex < LeftItems.Count && _lastLeftSelectedIndex >= 0) { // Автоматически переключаем фокус при достижении границ if (_leftSelectedIndex > _lastLeftSelectedIndex) { // Достигли конца списка - переходим к следующему элементу - MoveFocus(moveForward: true); + MoveFocus(moveForward: true, isLeft: true); } else if (_leftSelectedIndex < _lastLeftSelectedIndex) { // Достигли начала списка - переходим к предыдущему элементу - MoveFocus(moveForward: false); + MoveFocus(moveForward: false, isLeft: true); } } @@ -241,23 +258,23 @@ public partial class MainPage : ContentPage, INotifyPropertyChanged _rightSelectedIndex = RightItems.IndexOf(selectedItem); _isLeftPanelActive = false; - System.Diagnostics.Debug.WriteLine($"R !!! {_rightSelectedIndex} {_lastRightSelectedIndex}"); + // System.Diagnostics.Debug.WriteLine($"R !!! {_rightSelectedIndex} {_lastRightSelectedIndex}"); if (_lastRightSelectedIndex < RightItems.Count && _lastRightSelectedIndex >= 0) { // Автоматически переключаем фокус при достижении границ if (_rightSelectedIndex > _lastRightSelectedIndex) { // Достигли конца списка - переходим к следующему элементу - MoveFocus(moveForward: true); + MoveFocus(moveForward: true, isLeft: false); } else if (_rightSelectedIndex < _lastRightSelectedIndex) { // Достигли начала списка - переходим к предыдущему элементу - MoveFocus(moveForward: false); + MoveFocus(moveForward: false, isLeft: false); } } - UpdateVisualSelection(); + UpdateVisualSelection(); } }