Skip to content

Commit 61f336d

Browse files
authored
Merge pull request #72 from AvaloniaUtils/focusFixes
Fix popup focusing and focus escaping when no focusable controls in popup
2 parents c097656 + 772221b commit 61f336d

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

DialogHost.Avalonia/DialogOverlayPopupHost.axaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
TargetType="dialogHost:DialogOverlayPopupHost">
66
<Setter Property="Background" Value="{DynamicResource ThemeBackgroundBrush}" />
77
<Setter Property="KeyboardNavigation.TabNavigation" Value="Cycle" />
8+
<Setter Property="Focusable" Value="True" />
9+
<Setter Property="IsTabStop" Value="False" />
810
<Setter Property="Template">
911
<ControlTemplate>
1012
<ContentPresenter Name="PART_ContentPresenter"

DialogHost.Avalonia/DialogOverlayPopupHost.axaml.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,18 @@ private void UpdatePosition()
126126
}
127127

128128
public (bool handled, IInputElement? next) GetNext(IInputElement element, NavigationDirection direction) {
129+
// If current element isn't this popup host - ignoring
129130
if (!element.Equals(this)) {
130131
return (false, null);
131132
}
132-
var focusable = this.GetVisualDescendants().OfType<IInputElement>().FirstOrDefault(visual => visual.Focusable);
133-
return (true, focusable);
133+
134+
// Finding the focusable descendant
135+
var focusable = this.GetVisualDescendants()
136+
.OfType<IInputElement>()
137+
.FirstOrDefault(visual => visual.Focusable);
138+
139+
// Or returning the control itself to prevent focus escaping
140+
return (true, focusable ?? this);
134141
}
135142

136143
/// <inheritdoc />

0 commit comments

Comments
 (0)