Skip to content

New ItemClickBehavior for ListView/GridView.ItemClick #187

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Sergio0694
Copy link
Member

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 CommunityToolkit/WindowsCommunityToolkit#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...
    }
}

@Sergio0694 Sergio0694 changed the title Added ItemClickBehavior New ItemClickBehavior for ListView/GridView.ItemClick Jul 1, 2020
@jamesmcroft
Copy link
Member

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
Copy link
Member Author

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
Copy link
Member

jamesmcroft commented Jul 1, 2020

@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 (4c12ffe) by @DVaughan

@Sergio0694
Copy link
Member Author

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
Copy link
Collaborator

@mgoertz-msft what should be done here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants