Skip to content

ListDetailsView unfocus content on SelectedIndex changed. #4255

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ private void OnSelectedItemChanged(DependencyPropertyChangedEventArgs e)

OnSelectionChanged(new SelectionChangedEventArgs(new List<object> { e.OldValue }, new List<object> { e.NewValue }));
UpdateView(true);
SetFocus(ViewState);
}

private void OnLoaded(object sender, RoutedEventArgs e)
Expand Down Expand Up @@ -405,7 +406,7 @@ private void FocusFirstFocusableElementInDetails()
/// </summary>
private void FocusItemList()
{
if (GetTemplateChild("PartMainList") is Control list)
if (GetTemplateChild(PartMainList) is Control list)
{
list.Focus(FocusState.Programmatic);
}
Expand Down
112 changes: 112 additions & 0 deletions UnitTests/UnitTests.UWP/UI/Controls/Test_ListDetailsView_UI.cs
Original file line number Diff line number Diff line change
@@ -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 = @"<controls:ListDetailsView
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
xmlns:controls=""using:Microsoft.Toolkit.Uwp.UI.Controls""
NoSelectionContent=""No item selected"" >
<controls:ListDetailsView.ItemTemplate>
<DataTemplate>
<TextBlock Text=""Item"" />
</DataTemplate>
</controls:ListDetailsView.ItemTemplate>
<controls:ListDetailsView.DetailsTemplate>
<DataTemplate>
<TextBox Text=""{Binding}"" />
</DataTemplate>
</controls:ListDetailsView.DetailsTemplate>
</controls:ListDetailsView>";

[TestCategory("ListDetailsView")]
[TestMethod]
public async Task Test_LoseFocusOnNoSelection()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When first running both of these unit tests, they both failed. After running each of them in the Visual Studio debugger, and having them fail, again, they finally did pass, after the next pass.

{
await App.DispatcherQueue.EnqueueAsync(async () =>
{
var listDetailsView = XamlReader.Load(SampleXaml) as ListDetailsView;

listDetailsView.ItemsSource = new ObservableCollection<string>
{
"First",
};

listDetailsView.SelectedIndex = 0;

await SetTestContentAsync(listDetailsView);

var firsttb = listDetailsView.FindDescendant<TextBox>();

await App.DispatcherQueue.EnqueueAsync(() => firsttb.Focus(FocusState.Programmatic));

Assert.AreEqual(firsttb, FocusManager.GetFocusedElement(), "TextBox didn't get focus");

var tcs = new TaskCompletionSource<bool>();

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<string>
{
"First",
"Second",
};

listDetailsView.SelectedIndex = 0;

await SetTestContentAsync(listDetailsView);

var firsttb = listDetailsView.FindDescendant<TextBox>();

await App.DispatcherQueue.EnqueueAsync(() => firsttb.Focus(FocusState.Programmatic));

Assert.AreEqual(firsttb, FocusManager.GetFocusedElement(), "TextBox didn't get focus");

var tcs = new TaskCompletionSource<bool>();

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.");
});
}
}
}
1 change: 1 addition & 0 deletions UnitTests/UnitTests.UWP/UnitTests.UWP.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@
<Compile Include="UI\Controls\Test_ConstrainedBox.Multiple.cs" />
<Compile Include="UI\Controls\Test_ConstrainedBox.Combined.cs" />
<Compile Include="UI\Controls\Test_ImageEx.cs" />
<Compile Include="UI\Controls\Test_ListDetailsView_UI.cs" />
<Compile Include="UI\Controls\Test_RadialGauge.cs" />
<Compile Include="UI\Controls\Test_RichSuggestBox.cs" />
<Compile Include="UI\Controls\Test_TextToolbar_Localization.cs" />
Expand Down