question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Proposal] `ItemSelectedEventArgs` Converter

See original GitHub issue

ItemSelectedEventArgs Converter

  • Proposed
  • Prototype
  • Implementation
    • iOS Support
    • Android Support
    • macOS Support
    • Windows Support
  • Unit Tests
  • Sample
  • Documentation

Summary

The ItemSelectedEventArgsConverter is a converter that allows users to extract the SelectedItem value from an SelectedItemChangedEventArgs object. It can subsequently be used in combination with EventToCommandBehavior

Detailed Design

ItemSelectedEventArgsConverter.shared.cs

public class ItemSelectedEventArgsConverter : ValueConverterExtension, IValueConverter
{
  public object? Convert(object? value, Type? targetType, object? parameter, CultureInfo? culture)
  {
    if (value == null)
      return null;
    
    return value is SelectedItemChangedEventArgs selectedItemChangedEventArgs
		? selectedItemChangedEventArgs.SelectedItem
		: throw new ArgumentException("Expected value to be of type SelectedItemChangedEventArgs", nameof(value));
  }

  public object? ConvertBack(object? value, Type? targetType, object? parameter, CultureInfo? culture) => throw new NotImplementedException();
}

Usage Syntax

XAML Usage

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:xct="http://xamarin.com/schemas/2020/toolkit"
             x:Class="MyLittleApp.MainPage">

    <ContentPage.Resources>
        <ResourceDictionary>
            <xct:ItemSelectedEventArgsConverter x:Key="ItemSelectedEventArgsConverter" />
        </ResourceDictionary>
    </ContentPage.Resources>

    <ListView ItemsSource="{Binding Items}" HasUnevenRows="True">

        <ListView.Behaviors>
            <xct:EventToCommandBehavior EventName="ItemSelected"
                                              Command="{Binding ItemSelectedCommand}"
                                              EventArgsConverter="{StaticResource ItemSelectedEventArgsConverter}" />
        </ListView.Behaviors>

    </ListView>
</ContentPage>

C# Usage

class MyPage : ContentPage
{
  public MyPage()
  {
    var behavior = new EventToCommandBehavior
    {
      EventName = nameof(ListView.ItemSelected),
      EventArgsConverter = new ItemSelectedEventArgsConverter()
    };
    behavior.SetBinding(EventToCommandBehavior.CommandProperty, nameof(ViewModel.ItemSelectedCommand);

    var listView = new ListView { HasUnevenRows = true }.Bind(ListView.ItemsSource, nameof(ViewModel.Items));
    listView.Behaviors.Add(behavior);

    Content = listView;
  }
}

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
bijingtoncommented, Apr 25, 2022

Thanks again @CliffAgius 3 Proposals closed 😄

1reaction
CliffAgiuscommented, Apr 23, 2022

Can I have the docs for this one assigned to me please. 💪

Read more comments on GitHub >

github_iconTop Results From Across the Web

New Feature Proposals
The .NET MAUI Community Toolkit is a community-created library that contains .NET MAUI Extensions, Advanced UI/UX Controls, and Behaviors to help make your ......
Read more >
SelectedItemEventArgsConverter - .NET MAUI Community ...
The SelectedItemEventArgsConverter is a converter that allows users to extract the SelectedItem value from an SelectedItemChangedEventArgs ...
Read more >
Xamarin Community Forums - RSSing.com
Hi,. I'm following the Fonts Tutorial proposed by Xamarin to have a custom downloaded fonts over each plateform. However, I got lot of...
Read more >
Dropdown Control In Xamarin.Forms - Part One
Set Items Source from Xamarin.Forms Dropdown to Android Spinner control using array adapter, as shown in below. var view = e.NewElement; ...
Read more >
launch spinner on click of a button - xamarin.android
I want a spinner to show up on a button click. You could put a Button on Spinner by using a FrameLayout :...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found