Skip to content

Commit e146ab5

Browse files
Merge pull request #4255 from RosarioPulella/bugfix/listdetailsview-unfocus-content
ListDetailsView unfocus content on SelectedIndex changed.
2 parents f0209a3 + 394fbdb commit e146ab5

File tree

3 files changed

+115
-1
lines changed

3 files changed

+115
-1
lines changed

Microsoft.Toolkit.Uwp.UI.Controls.Layout/ListDetailsView/ListDetailsView.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ private void OnSelectedItemChanged(DependencyPropertyChangedEventArgs e)
163163

164164
OnSelectionChanged(new SelectionChangedEventArgs(new List<object> { e.OldValue }, new List<object> { e.NewValue }));
165165
UpdateView(true);
166+
SetFocus(ViewState);
166167
}
167168

168169
private void OnLoaded(object sender, RoutedEventArgs e)
@@ -405,7 +406,7 @@ private void FocusFirstFocusableElementInDetails()
405406
/// </summary>
406407
private void FocusItemList()
407408
{
408-
if (GetTemplateChild("PartMainList") is Control list)
409+
if (GetTemplateChild(PartMainList) is Control list)
409410
{
410411
list.Focus(FocusState.Programmatic);
411412
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using Microsoft.Toolkit.Uwp;
6+
using Microsoft.Toolkit.Uwp.UI;
7+
using Microsoft.Toolkit.Uwp.UI.Controls;
8+
using Microsoft.VisualStudio.TestTools.UnitTesting;
9+
using System;
10+
using System.Collections.ObjectModel;
11+
using System.Threading.Tasks;
12+
using Windows.UI.Xaml;
13+
using Windows.UI.Xaml.Controls;
14+
using Windows.UI.Xaml.Input;
15+
using Windows.UI.Xaml.Markup;
16+
17+
namespace UnitTests.UWP.UI.Controls
18+
{
19+
[TestClass]
20+
public class Test_ListDetailsView_UI : VisualUITestBase
21+
{
22+
private const string SampleXaml = @"<controls:ListDetailsView
23+
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
24+
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
25+
xmlns:controls=""using:Microsoft.Toolkit.Uwp.UI.Controls""
26+
NoSelectionContent=""No item selected"" >
27+
<controls:ListDetailsView.ItemTemplate>
28+
<DataTemplate>
29+
<TextBlock Text=""Item"" />
30+
</DataTemplate>
31+
</controls:ListDetailsView.ItemTemplate>
32+
<controls:ListDetailsView.DetailsTemplate>
33+
<DataTemplate>
34+
<TextBox Text=""{Binding}"" />
35+
</DataTemplate>
36+
</controls:ListDetailsView.DetailsTemplate>
37+
</controls:ListDetailsView>";
38+
39+
[TestCategory("ListDetailsView")]
40+
[TestMethod]
41+
public async Task Test_LoseFocusOnNoSelection()
42+
{
43+
await App.DispatcherQueue.EnqueueAsync(async () =>
44+
{
45+
var listDetailsView = XamlReader.Load(SampleXaml) as ListDetailsView;
46+
47+
listDetailsView.ItemsSource = new ObservableCollection<string>
48+
{
49+
"First",
50+
};
51+
52+
listDetailsView.SelectedIndex = 0;
53+
54+
await SetTestContentAsync(listDetailsView);
55+
56+
var firsttb = listDetailsView.FindDescendant<TextBox>();
57+
58+
await App.DispatcherQueue.EnqueueAsync(() => firsttb.Focus(FocusState.Programmatic));
59+
60+
Assert.AreEqual(firsttb, FocusManager.GetFocusedElement(), "TextBox didn't get focus");
61+
62+
var tcs = new TaskCompletionSource<bool>();
63+
64+
firsttb.LostFocus += (s, e) => tcs.SetResult(true);
65+
66+
listDetailsView.SelectedIndex = -1;
67+
68+
await Task.WhenAny(tcs.Task, Task.Delay(2000));
69+
70+
Assert.IsTrue(tcs.Task.IsCompleted);
71+
Assert.IsTrue(tcs.Task.Result, "TextBox in the first item should have lost focus.");
72+
});
73+
}
74+
75+
[TestCategory("ListDetailsView")]
76+
[TestMethod]
77+
public async Task Test_LoseFocusOnSelectOther()
78+
{
79+
await App.DispatcherQueue.EnqueueAsync(async () =>
80+
{
81+
var listDetailsView = XamlReader.Load(SampleXaml) as ListDetailsView;
82+
83+
listDetailsView.ItemsSource = new ObservableCollection<string>
84+
{
85+
"First",
86+
"Second",
87+
};
88+
89+
listDetailsView.SelectedIndex = 0;
90+
91+
await SetTestContentAsync(listDetailsView);
92+
93+
var firsttb = listDetailsView.FindDescendant<TextBox>();
94+
95+
await App.DispatcherQueue.EnqueueAsync(() => firsttb.Focus(FocusState.Programmatic));
96+
97+
Assert.AreEqual(firsttb, FocusManager.GetFocusedElement(), "TextBox didn't get focus");
98+
99+
var tcs = new TaskCompletionSource<bool>();
100+
101+
firsttb.LostFocus += (s, e) => tcs.SetResult(true);
102+
103+
listDetailsView.SelectedIndex = 1;
104+
105+
await Task.WhenAny(tcs.Task, Task.Delay(2000));
106+
107+
Assert.IsTrue(tcs.Task.IsCompleted);
108+
Assert.IsTrue(tcs.Task.Result, "TextBox in the first item should have lost focus.");
109+
});
110+
}
111+
}
112+
}

UnitTests/UnitTests.UWP/UnitTests.UWP.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@
237237
<Compile Include="UI\Controls\Test_ConstrainedBox.Multiple.cs" />
238238
<Compile Include="UI\Controls\Test_ConstrainedBox.Combined.cs" />
239239
<Compile Include="UI\Controls\Test_ImageEx.cs" />
240+
<Compile Include="UI\Controls\Test_ListDetailsView_UI.cs" />
240241
<Compile Include="UI\Controls\Test_RadialGauge.cs" />
241242
<Compile Include="UI\Controls\Test_RichSuggestBox.cs" />
242243
<Compile Include="UI\Controls\Test_TextToolbar_Localization.cs" />

0 commit comments

Comments
 (0)