Hello,
This is quite simple task but I am running short on ideas how to implement it. I have added FontAwesome to my app and would like to create a picker control that would display two different labels with different fonts side by side
You can do this by Entry and a popup layout.
No need to use handler and custom picker's popup window for every platforms.
Here are steps.
When the Entry get focus, it will popup a window like the Picker.
<Entry Focused="Entry_Focused"/>
I set entry.IsReadOnly = true;, it will not pop up keyboard for android/ios platforms. Then popup a window. And popup will return listview selected item.
Here is Entry's background code.
private async void Entry_Focused(object sender, FocusEventArgs e)
{
var popup = new PopUpPicker();
if (e.IsFocused)
{
Entry entry = sender as Entry;
entry.IsReadOnly = true;
entry.Unfocus();
var result = await this.ShowPopupAsync(popup, CancellationToken.None);
if (result is PickerItem stringResult)
{
entry.Text = stringResult.Text;
}
entry.IsReadOnly = false;
}
}
3.Here is my popup layout, you can custom it by your need.
<?xml version="1.0" encoding="utf-8" ?>
<toolkit:Popup xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
x:Class="MauiApp9.PopUpPicker"
>
<VerticalStackLayout>
<ListView ItemsSource="{Binding Items}" SelectionMode="Single" ItemSelected="ListView_ItemSelected">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<HorizontalStackLayout>
<Label Text="{Binding Icon}" FontFamily="FontAwesomeSolid" FontSize="20" VerticalTextAlignment="Center" Margin="5"/>
<Label Text="{Binding Text}" VerticalTextAlignment="Center" Margin="5"/>
</HorizontalStackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</VerticalStackLayout>
</toolkit:Popup>
4.After you selected an item in the listview, this popup window will be closed.
public partial class PopUpPicker : Popup
{
public PopUpPicker()
{
InitializeComponent();
}
private void ListView_ItemSelected(object sender, SelectedItemChangedEventArgs e)
{
CloseAsync(e.SelectedItem);
}
}
I can reproduce the issue, when I open the picker, I get following error. BindingContext cannot be found in the loaded xaml.
Error: BindingExpression path error: 'Icon' property not found on 'Windows.Foundation.IReference`1<String>'. BindingExpression: Path='Icon' DataItem='Windows.Foundation.IReference`1<String>'; target element is 'Microsoft.UI.Xaml.Controls.TextBlock' (Name='null'); target property is 'Text' (type 'String')
Best Regards,
Leon Lu
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
**Note: Please follow the steps in our [documenta