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..2aac79cf097 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsPage.xaml +++ b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsPage.xaml @@ -1,31 +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 2c14ae2df99..af5f546e65c 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsPage.xaml.cs +++ b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsPage.xaml.cs @@ -2,7 +2,12 @@ // 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.Windows.Input; +using Microsoft.Toolkit.Uwp.SampleApp.Common; +using Microsoft.Toolkit.Uwp.SampleApp.Data; using Microsoft.Toolkit.Uwp.UI.Extensions; +using Windows.UI.Popups; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; @@ -15,6 +20,8 @@ public ListViewExtensionsPage() this.InitializeComponent(); } + public ICommand SampleCommand => new DelegateCommand(OnExecuteSampleCommand); + public async void OnXamlRendered(FrameworkElement control) { var sampleListView = control.FindChildByName("SampleListView") as ListView; @@ -23,6 +30,14 @@ public async void OnXamlRendered(FrameworkElement control) { sampleListView.ItemsSource = await new Data.PhotosDataSource().GetItemsAsync(); } + + // Transfer Data Context so we can access SampleCommand + control.DataContext = this; + } + + 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.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 7840674f7ae..626fa1a9b98 100644 --- a/Microsoft.Toolkit.Uwp.UI/Extensions/ListViewBase/ListViewExtensions.cs +++ b/Microsoft.Toolkit.Uwp.UI/Extensions/ListViewBase/ListViewExtensions.cs @@ -118,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); }