From c1ca11013149240dbcc86b016e5b9310b777ec00 Mon Sep 17 00:00:00 2001 From: Dean Chalk Date: Wed, 1 Apr 2020 13:45:22 +0100 Subject: [PATCH 1/4] Addind Command attached property to ListViewExtensions so that the code is consistent with the docs --- .../ListViewExtensionsCode.bind | 2 + .../ListViewExtensionsPage.xaml | 1 + .../ListViewExtensionsPage.xaml.cs | 14 ++++ .../ListViewBase/ListViewExtensions.cs | 64 +++++++++++++++++++ 4 files changed, 81 insertions(+) diff --git a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsCode.bind b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsCode.bind index 85414268055..531b584d198 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsCode.bind +++ b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsCode.bind @@ -22,6 +22,8 @@ x:Name="SampleListView" Margin="12" ItemTemplate="{StaticResource NormalTemplate}" + IsItemClickEnabled="True" + extensions:ListViewExtensions.Command="{Binding SampleCommand}" extensions:ListViewExtensions.AlternateColor="#33AAAAAA" extensions:ListViewExtensions.AlternateItemTemplate="{StaticResource AlternateTemplate}" extensions:ListViewExtensions.StretchItemContainerDirection="Both"> diff --git a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsPage.xaml b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsPage.xaml index b36b13b86ec..1a0b715dc9d 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsPage.xaml +++ b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsPage.xaml @@ -22,6 +22,7 @@ new DelegateCommand(OnExecuteSampleCommand); + public async void OnXamlRendered(FrameworkElement control) { var sampleListView = control.FindChildByName("SampleListView") as ListView; @@ -22,7 +30,13 @@ public async void OnXamlRendered(FrameworkElement control) if (sampleListView != null) { sampleListView.ItemsSource = await new Data.PhotosDataSource().GetItemsAsync(); + ListViewExtensions.SetCommand(sampleListView, SampleCommand); } } + + private async void OnExecuteSampleCommand(PhotoDataItem item) + { + await new MessageDialog($"You clicked {item.Title} via the 'ListViewExtensions.Command' binding", "Item Clicked").ShowAsync(); + } } } diff --git a/Microsoft.Toolkit.Uwp.UI/Extensions/ListViewBase/ListViewExtensions.cs b/Microsoft.Toolkit.Uwp.UI/Extensions/ListViewBase/ListViewExtensions.cs index 5a26bf43b25..dca0bb7e524 100644 --- a/Microsoft.Toolkit.Uwp.UI/Extensions/ListViewBase/ListViewExtensions.cs +++ b/Microsoft.Toolkit.Uwp.UI/Extensions/ListViewBase/ListViewExtensions.cs @@ -2,7 +2,9 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using System; using System.Collections.Generic; +using System.Windows.Input; using Windows.Foundation.Collections; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; @@ -33,6 +35,11 @@ public static class ListViewExtensions /// public static readonly DependencyProperty StretchItemContainerDirectionProperty = DependencyProperty.RegisterAttached("StretchItemContainerDirection", typeof(StretchDirection), typeof(ListViewExtensions), new PropertyMetadata(null, OnStretchItemContainerDirectionPropertyChanged)); + /// + /// Attached for binding an to handle ListViewBase Item interaction by means of ItemClick event. ListViewBase IsItemClickEnabled must be set to true. + /// + public static readonly DependencyProperty CommandProperty = DependencyProperty.RegisterAttached("Command", typeof(ICommand), typeof(ListViewExtensions), new PropertyMetadata(null, OnCommandPropertyChanged)); + /// /// Gets the alternate associated with the specified /// @@ -93,6 +100,26 @@ public static void SetStretchItemContainerDirection(Windows.UI.Xaml.Controls.Lis obj.SetValue(StretchItemContainerDirectionProperty, value); } + /// + /// Gets the associated with the specified + /// + /// The to get the associated from + /// The associated with the + public static ICommand GetCommand(ListViewBase obj) + { + return (ICommand)obj.GetValue(CommandProperty); + } + + /// + /// Sets the associated with the specified + /// + /// The to associate the with + /// The for binding to the + public static void SetCommand(ListViewBase obj, ICommand value) + { + obj.SetValue(CommandProperty, value); + } + private static void OnAlternateColorPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args) { Windows.UI.Xaml.Controls.ListViewBase listViewBase = sender as Windows.UI.Xaml.Controls.ListViewBase; @@ -174,6 +201,28 @@ private static void OnStretchItemContainerDirectionPropertyChanged(DependencyObj } } + private static void OnCommandPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args) + { + var listViewBase = sender as ListViewBase; + + if (listViewBase == null) + { + return; + } + + var oldCommand = args.OldValue as ICommand; + if (oldCommand != null) + { + listViewBase.ItemClick -= OnListViewBaseItemClick; + } + + var newCommand = args.NewValue as ICommand; + if (newCommand != null) + { + listViewBase.ItemClick += OnListViewBaseItemClick; + } + } + private static void StretchItemContainerDirectionChanging(Windows.UI.Xaml.Controls.ListViewBase sender, ContainerContentChangingEventArgs args) { var itemContainer = args.ItemContainer as SelectorItem; @@ -190,6 +239,21 @@ private static void StretchItemContainerDirectionChanging(Windows.UI.Xaml.Contro } } + private static void OnListViewBaseItemClick(object sender, ItemClickEventArgs e) + { + var listViewBase = sender as ListViewBase; + var command = GetCommand(listViewBase); + if (listViewBase == null || command == null) + { + return; + } + + if (command.CanExecute(e.ClickedItem)) + { + command.Execute(e.ClickedItem); + } + } + private static void OnListViewBaseUnloaded(object sender, RoutedEventArgs e) { Windows.UI.Xaml.Controls.ListViewBase listViewBase = sender as Windows.UI.Xaml.Controls.ListViewBase; From c53cd166222fcfba63c5403e79d1e6bc948ea980 Mon Sep 17 00:00:00 2001 From: Dean Chalk Date: Wed, 1 Apr 2020 13:46:17 +0100 Subject: [PATCH 2/4] whitespace cleanup --- .../ListViewExtensions/ListViewExtensionsPage.xaml.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsPage.xaml.cs b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsPage.xaml.cs index 003da85a218..9d48e14fc75 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsPage.xaml.cs +++ b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsPage.xaml.cs @@ -18,7 +18,6 @@ public sealed partial class ListViewExtensionsPage : Page, IXamlRenderListener public ListViewExtensionsPage() { this.InitializeComponent(); - } public ICommand SampleCommand => new DelegateCommand(OnExecuteSampleCommand); From c1695f393fdfa4cf2ee34bd09da7badc7a72ad38 Mon Sep 17 00:00:00 2001 From: Dean Chalk Date: Mon, 27 Apr 2020 07:33:13 +0100 Subject: [PATCH 3/4] #3209 - updates from comments --- .../ListViewExtensionsPage.xaml | 38 ++++----- .../ListViewExtensionsPage.xaml.cs | 3 + .../ListViewExtensions.Command.cs | 78 +++++++++++++++++++ .../ListViewBase/ListViewExtensions.cs | 67 +--------------- 4 files changed, 101 insertions(+), 85 deletions(-) create mode 100644 Microsoft.Toolkit.Uwp.UI/Extensions/ListViewBase/ListViewExtensions.Command.cs diff --git a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsPage.xaml b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsPage.xaml index 1a0b715dc9d..2aac79cf097 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsPage.xaml +++ b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsPage.xaml @@ -1,32 +1,32 @@ - + - + - + - - + \ No newline at end of file diff --git a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsPage.xaml.cs b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsPage.xaml.cs index 9d48e14fc75..25bdbcab500 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsPage.xaml.cs +++ b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsPage.xaml.cs @@ -31,6 +31,9 @@ public async void OnXamlRendered(FrameworkElement control) sampleListView.ItemsSource = await new Data.PhotosDataSource().GetItemsAsync(); ListViewExtensions.SetCommand(sampleListView, SampleCommand); } + + // Transfer Data Context so we can access SampleCommand + control.DataContext = this; } private async void OnExecuteSampleCommand(PhotoDataItem item) diff --git a/Microsoft.Toolkit.Uwp.UI/Extensions/ListViewBase/ListViewExtensions.Command.cs b/Microsoft.Toolkit.Uwp.UI/Extensions/ListViewBase/ListViewExtensions.Command.cs new file mode 100644 index 00000000000..57d6ba7171a --- /dev/null +++ b/Microsoft.Toolkit.Uwp.UI/Extensions/ListViewBase/ListViewExtensions.Command.cs @@ -0,0 +1,78 @@ +// 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 System.Windows.Input; +using Windows.UI.Xaml; +using Windows.UI.Xaml.Controls; + +namespace Microsoft.Toolkit.Uwp.UI.Extensions +{ + /// + /// Provides the Command attached dependency property for the + /// + public static partial class ListViewExtensions + { + /// + /// Attached for binding an to handle ListViewBase Item interaction by means of ItemClick event. ListViewBase IsItemClickEnabled must be set to true. + /// + public static readonly DependencyProperty CommandProperty = DependencyProperty.RegisterAttached("Command", typeof(ICommand), typeof(ListViewExtensions), new PropertyMetadata(null, OnCommandPropertyChanged)); + + /// + /// Gets the associated with the specified + /// + /// The to get the associated from + /// The associated with the + public static ICommand GetCommand(ListViewBase obj) + { + return (ICommand)obj.GetValue(CommandProperty); + } + + /// + /// Sets the associated with the specified + /// + /// The to associate the with + /// The for binding to the + public static void SetCommand(ListViewBase obj, ICommand value) + { + obj.SetValue(CommandProperty, value); + } + + private static void OnCommandPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args) + { + var listViewBase = sender as ListViewBase; + + if (listViewBase == null) + { + return; + } + + var oldCommand = args.OldValue as ICommand; + if (oldCommand != null) + { + listViewBase.ItemClick -= OnListViewBaseItemClick; + } + + var newCommand = args.NewValue as ICommand; + if (newCommand != null) + { + listViewBase.ItemClick += OnListViewBaseItemClick; + } + } + + private static void OnListViewBaseItemClick(object sender, ItemClickEventArgs e) + { + var listViewBase = sender as ListViewBase; + var command = GetCommand(listViewBase); + if (listViewBase == null || command == null) + { + return; + } + + if (command.CanExecute(e.ClickedItem)) + { + command.Execute(e.ClickedItem); + } + } + } +} diff --git a/Microsoft.Toolkit.Uwp.UI/Extensions/ListViewBase/ListViewExtensions.cs b/Microsoft.Toolkit.Uwp.UI/Extensions/ListViewBase/ListViewExtensions.cs index dca0bb7e524..626fa1a9b98 100644 --- a/Microsoft.Toolkit.Uwp.UI/Extensions/ListViewBase/ListViewExtensions.cs +++ b/Microsoft.Toolkit.Uwp.UI/Extensions/ListViewBase/ListViewExtensions.cs @@ -2,9 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -using System; using System.Collections.Generic; -using System.Windows.Input; using Windows.Foundation.Collections; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; @@ -16,7 +14,7 @@ namespace Microsoft.Toolkit.Uwp.UI.Extensions /// /// Provides attached dependency properties for the /// - public static class ListViewExtensions + public static partial class ListViewExtensions { private static Dictionary, Windows.UI.Xaml.Controls.ListViewBase> _itemsForList = new Dictionary, Windows.UI.Xaml.Controls.ListViewBase>(); @@ -35,11 +33,6 @@ public static class ListViewExtensions /// public static readonly DependencyProperty StretchItemContainerDirectionProperty = DependencyProperty.RegisterAttached("StretchItemContainerDirection", typeof(StretchDirection), typeof(ListViewExtensions), new PropertyMetadata(null, OnStretchItemContainerDirectionPropertyChanged)); - /// - /// Attached for binding an to handle ListViewBase Item interaction by means of ItemClick event. ListViewBase IsItemClickEnabled must be set to true. - /// - public static readonly DependencyProperty CommandProperty = DependencyProperty.RegisterAttached("Command", typeof(ICommand), typeof(ListViewExtensions), new PropertyMetadata(null, OnCommandPropertyChanged)); - /// /// Gets the alternate associated with the specified /// @@ -100,26 +93,6 @@ public static void SetStretchItemContainerDirection(Windows.UI.Xaml.Controls.Lis obj.SetValue(StretchItemContainerDirectionProperty, value); } - /// - /// Gets the associated with the specified - /// - /// The to get the associated from - /// The associated with the - public static ICommand GetCommand(ListViewBase obj) - { - return (ICommand)obj.GetValue(CommandProperty); - } - - /// - /// Sets the associated with the specified - /// - /// The to associate the with - /// The for binding to the - public static void SetCommand(ListViewBase obj, ICommand value) - { - obj.SetValue(CommandProperty, value); - } - private static void OnAlternateColorPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args) { Windows.UI.Xaml.Controls.ListViewBase listViewBase = sender as Windows.UI.Xaml.Controls.ListViewBase; @@ -145,7 +118,6 @@ private static void OnAlternateColorPropertyChanged(DependencyObject sender, Dep private static void ColorContainerContentChanging(Windows.UI.Xaml.Controls.ListViewBase sender, ContainerContentChangingEventArgs args) { var itemContainer = args.ItemContainer as Control; - SetItemContainerBackground(sender, itemContainer, args.ItemIndex); } @@ -201,28 +173,6 @@ private static void OnStretchItemContainerDirectionPropertyChanged(DependencyObj } } - private static void OnCommandPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args) - { - var listViewBase = sender as ListViewBase; - - if (listViewBase == null) - { - return; - } - - var oldCommand = args.OldValue as ICommand; - if (oldCommand != null) - { - listViewBase.ItemClick -= OnListViewBaseItemClick; - } - - var newCommand = args.NewValue as ICommand; - if (newCommand != null) - { - listViewBase.ItemClick += OnListViewBaseItemClick; - } - } - private static void StretchItemContainerDirectionChanging(Windows.UI.Xaml.Controls.ListViewBase sender, ContainerContentChangingEventArgs args) { var itemContainer = args.ItemContainer as SelectorItem; @@ -239,21 +189,6 @@ private static void StretchItemContainerDirectionChanging(Windows.UI.Xaml.Contro } } - private static void OnListViewBaseItemClick(object sender, ItemClickEventArgs e) - { - var listViewBase = sender as ListViewBase; - var command = GetCommand(listViewBase); - if (listViewBase == null || command == null) - { - return; - } - - if (command.CanExecute(e.ClickedItem)) - { - command.Execute(e.ClickedItem); - } - } - private static void OnListViewBaseUnloaded(object sender, RoutedEventArgs e) { Windows.UI.Xaml.Controls.ListViewBase listViewBase = sender as Windows.UI.Xaml.Controls.ListViewBase; From 3c805aac5350afb650c90135ce90d8c31d2a949c Mon Sep 17 00:00:00 2001 From: "Michael Hawker MSFT (XAML Llama)" <24302614+michael-hawker@users.noreply.github.com> Date: Thu, 28 May 2020 13:31:29 -0700 Subject: [PATCH 4/4] Update Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsPage.xaml.cs Tested removing this line, as duplicated logic, done through binding. --- .../ListViewExtensions/ListViewExtensionsPage.xaml.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsPage.xaml.cs b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsPage.xaml.cs index 25bdbcab500..af5f546e65c 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsPage.xaml.cs +++ b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsPage.xaml.cs @@ -29,7 +29,6 @@ public async void OnXamlRendered(FrameworkElement control) if (sampleListView != null) { sampleListView.ItemsSource = await new Data.PhotosDataSource().GetItemsAsync(); - ListViewExtensions.SetCommand(sampleListView, SampleCommand); } // Transfer Data Context so we can access SampleCommand