Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
[WinRT/UWP] Open Picker dropdown when calling Focus
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul DiPietro committed Mar 19, 2017
1 parent ce06819 commit 984a4c0
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using Xamarin.Forms.CustomAttributes;
using Xamarin.Forms.Internals;

#if UITEST
using Xamarin.UITest;
using NUnit.Framework;
#endif

namespace Xamarin.Forms.Controls.Issues
{
[Preserve(AllMembers = true)]
[Issue(IssueTracker.Bugzilla, 52266, "[WinRT/UWP] Picker.Focus() does not open the dropdown", PlatformAffected.WinRT)]
public class Bugzilla52266 : TestContentPage
{
protected override void Init()
{
var picker = new Picker
{
ItemsSource = new string[] { "A", "B", "C" }
};
Content = new StackLayout
{
Children =
{
picker,
new Button
{
Text = "Click to focus the picker",
Command = new Command(() =>
{
picker.Focus();
})
}
}
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@
<DependentUpon>Bugzilla51642.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)Bugzilla52266.cs" />
<Compile Include="$(MSBuildThisFileDirectory)CarouselAsync.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Bugzilla34561.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Bugzilla34727.cs" />
Expand Down
19 changes: 19 additions & 0 deletions Xamarin.Forms.Platform.WinRT/PickerRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class PickerRenderer : ViewRenderer<Picker, FormsComboBox>
{
bool _isAnimating;
Brush _defaultBrush;
bool _dropDownWasOpened;

protected override void Dispose(bool disposing)
{
Expand All @@ -32,6 +33,7 @@ protected override void Dispose(bool disposing)
Control.DropDownClosed -= OnDropDownOpenStateChanged;
Control.OpenAnimationCompleted -= ControlOnOpenAnimationCompleted;
Control.Loaded -= ControlOnLoaded;
Control.GotFocus -= ControlOnGotFocus;
}
}

Expand All @@ -51,6 +53,7 @@ protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
Control.OpenAnimationCompleted += ControlOnOpenAnimationCompleted;
Control.ClosedAnimationStarted += ControlOnClosedAnimationStarted;
Control.Loaded += ControlOnLoaded;
Control.GotFocus += ControlOnGotFocus;
}

Control.ItemsSource = ((LockableObservableListWrapper)Element.Items)._list;
Expand Down Expand Up @@ -102,6 +105,19 @@ void ControlOnOpenAnimationCompleted(object sender, EventArgs eventArgs)
}
}

void ControlOnGotFocus(object sender, RoutedEventArgs routedEventArgs)
{
// The FormsComboBox is separate from the Popup/dropdown that it uses to select an item,
// and the behavior here is changed to be similar to the other platforms where focusing the
// Picker opens the dropdown (with the exception where if focus was given via keyboard, such
// as tabbing through controls). The _dropDownWasOpened flag is reset to false in the case that
// the FormsComboBox regained focus after the dropdown closed.
if (!_dropDownWasOpened && Control.FocusState != FocusState.Keyboard)
Control.IsDropDownOpen = true;
else
_dropDownWasOpened = false;
}

void OnControlSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (Element != null)
Expand Down Expand Up @@ -129,6 +145,9 @@ void OnDropDownOpenStateChanged(object sender, object o)
_isAnimating = false;
// and force the final redraw
((IVisualElementController)Element)?.InvalidateMeasure(InvalidationTrigger.MeasureChanged);

// Related to ControlOnGotFocus, _dropDownWasOpened is set to true
_dropDownWasOpened = true;
}
}

Expand Down

0 comments on commit 984a4c0

Please sign in to comment.