From 640e35526b3d7520610a7666d4b34962c3d66763 Mon Sep 17 00:00:00 2001 From: Paul DiPietro Date: Thu, 21 Dec 2017 14:32:48 -0500 Subject: [PATCH] [UWP] Set ListView SelectedItem when constructing page (#1357) --- .../Issue1356.cs | 37 +++++++++++++++++++ ...rin.Forms.Controls.Issues.Shared.projitems | 1 + .../ListViewRenderer.cs | 3 ++ 3 files changed, 41 insertions(+) create mode 100644 Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue1356.cs diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue1356.cs b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue1356.cs new file mode 100644 index 000000000..946b9d097 --- /dev/null +++ b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue1356.cs @@ -0,0 +1,37 @@ +using Xamarin.Forms.CustomAttributes; +using Xamarin.Forms.Internals; +using System.Collections.ObjectModel; + +#if UITEST +using Xamarin.UITest; +using NUnit.Framework; +#endif + +namespace Xamarin.Forms.Controls.Issues +{ + [Preserve(AllMembers = true)] + [Issue(IssueTracker.Github, 1356, "[UWP] A selected item in a ListView is not highlighted when constructing the page", PlatformAffected.UWP)] + public class Issue1356 : TestContentPage + { + ObservableCollection items = new ObservableCollection() { "A", "B", "C" }; + + ListView listView; + protected override void Init() + { + listView = new ListView + { + ItemsSource = items + }; + + Content = new StackLayout + { + Children = + { + listView, + new Label { Text = "Item 'B' should be highlighted when loading this page" } + } + }; + listView.SelectedItem = items[1]; + } + } +} \ No newline at end of file diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems index fc0d59417..e8db9afc3 100644 --- a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems +++ b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems @@ -256,6 +256,7 @@ + diff --git a/Xamarin.Forms.Platform.UAP/ListViewRenderer.cs b/Xamarin.Forms.Platform.UAP/ListViewRenderer.cs index c04aa6ec6..06126bfcc 100644 --- a/Xamarin.Forms.Platform.UAP/ListViewRenderer.cs +++ b/Xamarin.Forms.Platform.UAP/ListViewRenderer.cs @@ -64,6 +64,9 @@ namespace Xamarin.Forms.Platform.UWP // WinRT throws an exception if you set ItemsSource directly to a CVS, so bind it. List.DataContext = new CollectionViewSource { Source = Element.ItemsSource, IsSourceGrouped = Element.IsGroupingEnabled }; + if (Element.SelectedItem != null) + OnElementItemSelected(null, new SelectedItemChangedEventArgs(Element.SelectedItem)); + UpdateGrouping(); UpdateHeader(); UpdateFooter();