Skip to content
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

Update Picker to have Func for ItemDisplayBinding #461

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions samples/ControlGallery/Views/PickerPlayground.razor
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<Label Text="Picker Playground" FontSize="30">
</Label>

<BoxView HeightRequest="2" Color="Colors.Gray" HorizontalOptions="LayoutOptions.FillAndExpand" />
<BoxView HeightRequest="2" Color="Colors.Gray" HorizontalOptions="LayoutOptions.Fill" />

<Label Padding="5" FontAttributes="FontAttributes.Bold" Text="Picker with models and all display properties" />
<Picker CharacterSpacing="3"
Expand All @@ -22,34 +22,34 @@
Title="Select a model"
TitleColor="Colors.Blue"
VerticalTextAlignment="TextAlignment.End"
ItemDisplayBinding="Name"
ItemDisplayBinding="i => i.Name"
@bind-SelectedItem="SelectedModel">
</Picker>
<Label Padding="5" Text="@("Selected model: " + SelectedModel?.Name ?? "(none)")"></Label>

<BoxView HeightRequest="2" Color="Colors.Gray" HorizontalOptions="LayoutOptions.FillAndExpand" />
<BoxView HeightRequest="2" Color="Colors.Gray" HorizontalOptions="LayoutOptions.Fill" />

<Label FontAttributes="FontAttributes.Bold" Text="Picker with string" />
<Picker @bind-SelectedItem="SelectedString"
Title="Select a string"
ItemsSource="Strings" />
<Label Text="@("Selected string: " + SelectedString ?? "(none)")"></Label>

<BoxView HeightRequest="2" Color="Colors.Gray" HorizontalOptions="LayoutOptions.FillAndExpand" />
<BoxView HeightRequest="2" Color="Colors.Gray" HorizontalOptions="LayoutOptions.Fill" />

<Label FontAttributes="FontAttributes.Bold" Text="Picker with models and default item selected" />
<Picker @bind-SelectedItem="SelectedModelDefault"
Title="Select a model"
ItemDisplayBinding="Name"
ItemDisplayBinding="ItemDisplay"
ItemsSource="ModelsWithDefault" />
<Label Text="@("Selected model: " + SelectedModelDefault?.Name ?? "(none)")"></Label>

<BoxView HeightRequest="2" Color="Colors.Gray" HorizontalOptions="LayoutOptions.FillAndExpand" />
<BoxView HeightRequest="2" Color="Colors.Gray" HorizontalOptions="LayoutOptions.Fill" />

<Label FontAttributes="FontAttributes.Bold" Text="Index Based Picker" />
<Picker ItemsSource="Models"
Title="Select a model and the index will bind"
ItemDisplayBinding="Name"
ItemDisplayBinding="i => i.Name"
@bind-SelectedIndex="SelectedIndex">
</Picker>
<Label Text="@("Selected model: " + Models.ElementAtOrDefault(SelectedIndex)?.Name ?? "(none)")"></Label>
Expand Down Expand Up @@ -103,4 +103,9 @@
SelectedModelDefault = ModelsWithDefault.LastOrDefault();
SelectedIndex = -1;
}

public string ItemDisplay(PickerModel model)
{
return $"❤️{model.Name}❤️";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace Microsoft.MobileBlazorBindings.Elements.Handlers
{
public class PickerHandler : ViewHandler
public class PickerHandler<TItem> : ViewHandler
{
public PickerHandler(NativeComponentRenderer renderer, MC.Picker pickerControl) : base(renderer, pickerControl)
{
Expand Down Expand Up @@ -68,7 +68,8 @@ public override void ApplyAttribute(ulong attributeEventHandlerId, string attrib
PickerControl.HorizontalTextAlignment = (TextAlignment)AttributeHelper.GetInt(attributeValue);
break;
case nameof(MC.Picker.ItemDisplayBinding):
PickerControl.ItemDisplayBinding = new MC.Binding((string)attributeValue);
var func = (Func<TItem, string>)attributeValue;
PickerControl.ItemDisplayBinding = new MC.Internals.TypedBinding<TItem, string>((item) => (func(item), true), null, null);
break;
case nameof(MC.Picker.ItemsSource):
var items = AttributeHelper.DelegateToObject<IList>(attributeValue);
Expand Down
5 changes: 3 additions & 2 deletions src/Microsoft.MobileBlazorBindings/Elements/Picker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Microsoft.Maui.Graphics;
using Microsoft.MobileBlazorBindings.Core;
using Microsoft.MobileBlazorBindings.Elements.Handlers;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using MC = Microsoft.Maui.Controls;
Expand All @@ -21,7 +22,7 @@ public class Picker<TItem> : View
[Parameter] public IList<TItem> ItemsSource { get; set; }
#pragma warning restore CA2227 // Collection properties should be read only
[Parameter] public string Title { get; set; }
[Parameter] public string ItemDisplayBinding { get; set; }
[Parameter] public Func<TItem, string> ItemDisplayBinding { get; set; }
[Parameter] public TItem SelectedItem { get; set; }
[Parameter] public int SelectedIndex { get; set; } = -1;
[Parameter] public EventCallback<TItem> SelectedItemChanged { get; set; }
Expand Down Expand Up @@ -67,7 +68,7 @@ public class Picker<TItem> : View

static Picker()
{
ElementHandlerRegistry.RegisterElementHandler<Picker<TItem>>(renderer => new PickerHandler(renderer, new MC.Picker()));
ElementHandlerRegistry.RegisterElementHandler<Picker<TItem>>(renderer => new PickerHandler<TItem>(renderer, new MC.Picker()));
}

protected override void RenderAttributes(AttributesBuilder builder)
Expand Down