diff --git a/Microsoft.Toolkit.Uwp.UI.Controls.Layout/ListDetailsView/ListDetailsView.cs b/Microsoft.Toolkit.Uwp.UI.Controls.Layout/ListDetailsView/ListDetailsView.cs index 26b69afa100..c674540629b 100644 --- a/Microsoft.Toolkit.Uwp.UI.Controls.Layout/ListDetailsView/ListDetailsView.cs +++ b/Microsoft.Toolkit.Uwp.UI.Controls.Layout/ListDetailsView/ListDetailsView.cs @@ -163,6 +163,7 @@ private void OnSelectedItemChanged(DependencyPropertyChangedEventArgs e) OnSelectionChanged(new SelectionChangedEventArgs(new List { e.OldValue }, new List { e.NewValue })); UpdateView(true); + SetFocus(ViewState); } private void OnLoaded(object sender, RoutedEventArgs e) @@ -405,7 +406,7 @@ private void FocusFirstFocusableElementInDetails() /// private void FocusItemList() { - if (GetTemplateChild("PartMainList") is Control list) + if (GetTemplateChild(PartMainList) is Control list) { list.Focus(FocusState.Programmatic); } diff --git a/UnitTests/UnitTests.UWP/UI/Controls/Test_ListDetailsView_UI.cs b/UnitTests/UnitTests.UWP/UI/Controls/Test_ListDetailsView_UI.cs new file mode 100644 index 00000000000..96b6a08935e --- /dev/null +++ b/UnitTests/UnitTests.UWP/UI/Controls/Test_ListDetailsView_UI.cs @@ -0,0 +1,112 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using Microsoft.Toolkit.Uwp; +using Microsoft.Toolkit.Uwp.UI; +using Microsoft.Toolkit.Uwp.UI.Controls; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; +using System.Collections.ObjectModel; +using System.Threading.Tasks; +using Windows.UI.Xaml; +using Windows.UI.Xaml.Controls; +using Windows.UI.Xaml.Input; +using Windows.UI.Xaml.Markup; + +namespace UnitTests.UWP.UI.Controls +{ + [TestClass] + public class Test_ListDetailsView_UI : VisualUITestBase + { + private const string SampleXaml = @" + + + + + + + + + + + "; + + [TestCategory("ListDetailsView")] + [TestMethod] + public async Task Test_LoseFocusOnNoSelection() + { + await App.DispatcherQueue.EnqueueAsync(async () => + { + var listDetailsView = XamlReader.Load(SampleXaml) as ListDetailsView; + + listDetailsView.ItemsSource = new ObservableCollection + { + "First", + }; + + listDetailsView.SelectedIndex = 0; + + await SetTestContentAsync(listDetailsView); + + var firsttb = listDetailsView.FindDescendant(); + + await App.DispatcherQueue.EnqueueAsync(() => firsttb.Focus(FocusState.Programmatic)); + + Assert.AreEqual(firsttb, FocusManager.GetFocusedElement(), "TextBox didn't get focus"); + + var tcs = new TaskCompletionSource(); + + firsttb.LostFocus += (s, e) => tcs.SetResult(true); + + listDetailsView.SelectedIndex = -1; + + await Task.WhenAny(tcs.Task, Task.Delay(2000)); + + Assert.IsTrue(tcs.Task.IsCompleted); + Assert.IsTrue(tcs.Task.Result, "TextBox in the first item should have lost focus."); + }); + } + + [TestCategory("ListDetailsView")] + [TestMethod] + public async Task Test_LoseFocusOnSelectOther() + { + await App.DispatcherQueue.EnqueueAsync(async () => + { + var listDetailsView = XamlReader.Load(SampleXaml) as ListDetailsView; + + listDetailsView.ItemsSource = new ObservableCollection + { + "First", + "Second", + }; + + listDetailsView.SelectedIndex = 0; + + await SetTestContentAsync(listDetailsView); + + var firsttb = listDetailsView.FindDescendant(); + + await App.DispatcherQueue.EnqueueAsync(() => firsttb.Focus(FocusState.Programmatic)); + + Assert.AreEqual(firsttb, FocusManager.GetFocusedElement(), "TextBox didn't get focus"); + + var tcs = new TaskCompletionSource(); + + firsttb.LostFocus += (s, e) => tcs.SetResult(true); + + listDetailsView.SelectedIndex = 1; + + await Task.WhenAny(tcs.Task, Task.Delay(2000)); + + Assert.IsTrue(tcs.Task.IsCompleted); + Assert.IsTrue(tcs.Task.Result, "TextBox in the first item should have lost focus."); + }); + } + } +} diff --git a/UnitTests/UnitTests.UWP/UnitTests.UWP.csproj b/UnitTests/UnitTests.UWP/UnitTests.UWP.csproj index 20c6e4582ea..86b5da16c59 100644 --- a/UnitTests/UnitTests.UWP/UnitTests.UWP.csproj +++ b/UnitTests/UnitTests.UWP/UnitTests.UWP.csproj @@ -237,6 +237,7 @@ +