Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 58056

Expose ListView's ItemTapped event as ICommand via attached property

$
0
0

I'm trying to implement a couple of attached properties for ListView, especially to have ItemTapped event exposed as a command for binding.

That's what I have so far:

public class ListViewCommands
{
    public static readonly BindableProperty ItemTappedProperty =
        BindableProperty.CreateAttached<BindableObject, Command>(
            bindable => GetItemTapped(bindable),
            null,
            BindingMode.OneWay,
            null,
            (b, o, n) => OnItemTappedChanged(b, o, n),
            null,
            null);

    public static Command GetItemTapped(BindableObject bo)
    {
        return (Command)bo.GetValue(ItemTappedProperty);
    }

    public static void SetItemTapped(BindableObject bo, Command value)
    {
        bo.SetValue(ItemTappedProperty, value);
    }

    public static void OnItemTappedChanged(BindableObject bo, Command oldValue, Command newValue)
    {
        // Here I'm going to attach to the ListView's ItemTapped event.
    }
}

in XAML:

<ListView
  ItemsSource="{Binding MyItems}"
  my:ListViewCommands.ItemTapped="{Binding TappedCommand}" />

But I'm placing breakpoints inside the GetItemTapped/SetItemTapped/OnItemTappedCommand methods, but they actually not get called. All this thing just seems to be ignored.

The question:

1) What's specifically incorrect in my implementation of attached property?

2) Does it work in Xamarin Forms at all?


Viewing all articles
Browse latest Browse all 58056

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>