microsoft / XamlBehaviors

This is the official home for UWP XAML Behaviors on GitHub.
MIT License
697 stars 112 forks source link

New ItemClickBehavior for ListView/GridView.ItemClick #187

Open Sergio0694 opened 4 years ago

Sergio0694 commented 4 years ago

Feature: new ItemClickBehavior type

Summary

This PR introduces a new ItemClickBerhavior type, targeting ListViewBase (so it can be used with both ListView and GridView), which allows to easily invoke a command when an item is clicking, and also to pass the clicked item directly as parameter. This results in more compact and efficient code than using an EventTriggerBehavior + an invoke command behavior, and it allows to directly receive the clicked item as argument to the command, which would've otherwise not been possible.

Here's a usage example:

<ListView
    ItemsSource="{x:Bind ViewModel.Source}"
    IsItemClickEnabled="True">
    <interactivity:Interaction.Behaviors>
        <core:ItemClickBehavior Command="{x:Bind ViewModel.ProcessItemCommand}"/>
    </interactivity:Interaction.Behaviors>
    <ListView.ItemTemplate>
        <DataTemplate>
            <!--Template here...-->
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

The target command will be invoked when an item is clicked and will receive the ItemClickEventArgs.ClickedItem instance directly. This is especially useful when using eg. the RelayCommand<T> type (see https://github.com/windows-toolkit/WindowsCommunityToolkit/issues/3230), as you can then also specify the target type of a clicked item and have the command wrap a model directly in the viewmodel:

public class MyViewModel : ViewModelBase
{
    public MyViewModel()
    {
        Source = ...;
        ProcessItemCommand = new RelayCommand<MyModel>(ProcessItem);
    }

    public ObservableCollection<MyModel> Source { get; }

    public ICommand ProcessItemCommand { get; }

    private void ProcessItem(MyModel model)
    {
        // Custom logic here...
    }
}
jamesmcroft commented 4 years ago

I'm surprised this wasn't here already actually! Having created this on multiple occasions for various projects, I'd find this addition welcoming.

Sergio0694 commented 4 years ago

Can anyone help with the CI? I'm not super familiar with the setup of this repo/CI, but the Microsoft.Xaml.Interactions project is building just fine for me locally (granted, I needed to locally bump the SDK to 17763 since the original SDK 10586 is so old I don't even see that as an option in the Visual Studio installer). 😅

The CI is failing with some rather weird errors that seem a bit suspicious, considering the new behavior literally is a single self-contained class only using standard types from the BCL and the various built-in UWP types.

I mean, like:

TriggerOfT.cs(13,58): error CS1069: The type name 'DependencyObject' could not be found in the namespace 'Windows.UI.Xaml'. This type has been forwarded to assembly 'Windows, Version=255.255.255.255, Culture=neutral, PublicKeyToken=null, ContentType=WindowsRuntime' Consider adding a reference to that assembly. [C:\projects\xamlbehaviors\src\BehaviorsSDKManaged\Microsoft.Xaml.Interactivity\Microsoft.Xaml.Interactivity.csproj]

VisualStateUtilities.cs(46,68): error CS1069: The type name 'FrameworkElement' could not be found in the namespace 'Windows.UI.Xaml'. This type has been forwarded to assembly 'Windows, Version=255.255.255.255, Culture=neutral, PublicKeyToken=null, ContentType=WindowsRuntime' Consider adding a reference to that assembly. [C:\projects\xamlbehaviors\src\BehaviorsSDKManaged\Microsoft.Xaml.Interactivity\Microsoft.Xaml.Interactivity.csproj]

VisualStateUtilities.cs(78,58): error CS1069: The type name 'FrameworkElement' could not be found in the namespace 'Windows.UI.Xaml'. This type has been forwarded to assembly 'Windows, Version=255.255.255.255, Culture=neutral, PublicKeyToken=null, ContentType=WindowsRuntime' Consider adding a reference to that assembly. [C:\projects\xamlbehaviors\src\BehaviorsSDKManaged\Microsoft.Xaml.Interactivity\Microsoft.Xaml.Interactivity.csproj]

I'm a bit confused here 🤔

jamesmcroft commented 4 years ago

@Sergio0694 it looks like the issue is to do with the SDKs not being available and it's building with VS2019 looking at the logs from appveyor.

At the top,

C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Microsoft\WindowsXaml\v16.0\8.2\Microsoft.Windows.UI.Xaml.Common.targets(676,5): warning : Could not find target platform winmds for the SDK specified by [Windows, 10.0, UAP, 10.0.10240.0, 10.0.10586.0]

Then everything after that point is just errors 🤔

Looking at the build history too shows it's been failing for some time.

It almost looks like there was a change with the CI in appveyor to move from VS2017 (where it was previously building).

Also appears that the change you described to get it to work was exactly what had been done on the preview WinUI branch too (https://github.com/Microsoft/XamlBehaviors/commit/4c12ffe8aee017d90504a90ffd4393f5876e64fb) by @DVaughan

Sergio0694 commented 4 years ago

Hey @jamesmcroft - thanks for looking into this! Yeah that makes perfect sense, especially because I really didn't make any breaking changes or refactorings at all in the PR.

I guess we'll just need to hear from someone from the behaviors team about how to proceed here then 😄

azchohfi commented 4 years ago

@mgoertz-msft what should be done here?