[Proposal] `ItemSelectedEventArgs` Converter
See original GitHub issueItemSelectedEventArgs
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:
- Created 2 years ago
- Comments:5 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thanks again @CliffAgius 3 Proposals closed 😄
Can I have the docs for this one assigned to me please. 💪