Skip to content

Making the ListExtensions feature in-line with the documentation #3209

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
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<ListView
x:Name="SampleListView"
Margin="12"
IsItemClickEnabled="True"
ItemTemplate="{StaticResource NormalTemplate}"
extensions:ListViewExtensions.AlternateColor="#33AAAAAA"
extensions:ListViewExtensions.AlternateItemTemplate="{StaticResource AlternateTemplate}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -15,14 +20,22 @@ public ListViewExtensionsPage()
this.InitializeComponent();
}

public ICommand SampleCommand => new DelegateCommand<PhotoDataItem>(OnExecuteSampleCommand);

public async void OnXamlRendered(FrameworkElement control)
{
var sampleListView = control.FindChildByName("SampleListView") as ListView;

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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -33,6 +35,11 @@ public static class ListViewExtensions
/// </summary>
public static readonly DependencyProperty StretchItemContainerDirectionProperty = DependencyProperty.RegisterAttached("StretchItemContainerDirection", typeof(StretchDirection), typeof(ListViewExtensions), new PropertyMetadata(null, OnStretchItemContainerDirectionPropertyChanged));

/// <summary>
/// Attached <see cref="DependencyProperty"/> for binding an <see cref="System.Windows.Input.ICommand"/> to handle ListViewBase Item interaction by means of <see cref="Windows.UI.Xaml.Controls.ListViewBase"/> ItemClick event. ListViewBase IsItemClickEnabled must be set to true.
/// </summary>
public static readonly DependencyProperty CommandProperty = DependencyProperty.RegisterAttached("Command", typeof(ICommand), typeof(ListViewExtensions), new PropertyMetadata(null, OnCommandPropertyChanged));

/// <summary>
/// Gets the alternate <see cref="Brush"/> associated with the specified <see cref="Windows.UI.Xaml.Controls.ListViewBase"/>
/// </summary>
Expand Down Expand Up @@ -93,6 +100,26 @@ public static void SetStretchItemContainerDirection(Windows.UI.Xaml.Controls.Lis
obj.SetValue(StretchItemContainerDirectionProperty, value);
}

/// <summary>
/// Gets the <see cref="ICommand"/> associated with the specified <see cref="ListViewBase"/>
/// </summary>
/// <param name="obj">The <see cref="Windows.UI.Xaml.Controls.ListViewBase"/> to get the associated <see cref="ICommand"/> from</param>
/// <returns>The <see cref="ICommand"/> associated with the <see cref="ListViewBase"/></returns>
public static ICommand GetCommand(ListViewBase obj)
{
return (ICommand)obj.GetValue(CommandProperty);
}

/// <summary>
/// Sets the <see cref="ICommand"/> associated with the specified <see cref="ListViewBase"/>
/// </summary>
/// <param name="obj">The <see cref="Windows.UI.Xaml.Controls.ListViewBase"/> to associate the <see cref="ICommand"/> with</param>
/// <param name="value">The <see cref="ICommand"/> for binding to the <see cref="ListViewBase"/></param>
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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down