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.

Picker Title displays above the control on Windows

See original GitHub issue

Description

When running on Windows, the Picker displays the Title property above the control. According to the documentation, and the behaviour on Android, I expect it to be treated as a placeholder when no item is selected.

Documentation snippet: image

Windows: image

Android: image

Steps to Reproduce

  1. Create new MAUI app.
  2. Add a Picker to MainPage.xaml as below (direct copy from Microsoft’s sample code)
<Picker x:Name="picker"
        Title="Select a monkey">
  <Picker.ItemsSource>
    <x:Array Type="{x:Type x:String}">
      <x:String>Baboon</x:String>
      <x:String>Capuchin Monkey</x:String>
      <x:String>Blue Monkey</x:String>
      <x:String>Squirrel Monkey</x:String>
      <x:String>Golden Lion Tamarin</x:String>
      <x:String>Howler Monkey</x:String>
      <x:String>Japanese Macaque</x:String>
    </x:Array>
  </Picker.ItemsSource>
</Picker>
  1. Run on Windows Machine

Version with bug

Release Candidate 2 (current)

Last version that worked well

Unknown/Other

Affected platforms

Windows

Affected platform versions

Windows 10 (21H2 | 19044.1645)

Did you find any workaround?

No

Relevant log output

N/A

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:1
  • Comments:13 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
mkomelcommented, May 5, 2022

Setting pick.Title = null; after line 15 in @MitchBomcanhao 's code snippet seems to do the trick (to remove the header from the native view).

For completeness, I’m pasting the snippet here again:

#if WINDOWS10_0_19041_0_OR_GREATER
    Microsoft.Maui.Handlers.PickerHandler.Mapper.AppendToMapping(nameof(IPicker.Title), (handler, view) =>
    {
        if (handler.PlatformView is not null && view is Picker pick && !String.IsNullOrWhiteSpace(pick.Title))
        {
            handler.PlatformView.HeaderTemplate = new Microsoft.UI.Xaml.DataTemplate();			
            handler.PlatformView.PlaceholderText = pick.Title;
            pick.Title = null;
         }
    });
#endif
3reactions
WoMartcommented, May 6, 2022

@mkomel Thanks for the snippet, it works great. I think it’s worth noting that the TitleColor property will not be applied to the PlaceholderText, but it can be with the adjustment below.

using WindowsUI = Windows.UI;
using XamlUI = Microsoft.UI.Xaml.Media;

(...)

#if WINDOWS10_0_19041_0_OR_GREATER
  PickerHandler.Mapper.AppendToMapping(nameof(IPicker.Title), (handler, view) => {
      if (handler.PlatformView is not null && view is Picker pick && !string.IsNullOrWhiteSpace(pick.Title)) {
          handler.PlatformView.HeaderTemplate = new Microsoft.UI.Xaml.DataTemplate();
          handler.PlatformView.PlaceholderText = pick.Title;
          pick.Title = null;
  
          pick.TitleColor.ToRgba(out byte r, out byte g, out byte b, out byte a);
          handler.PlatformView.PlaceholderForeground = new XamlUI.SolidColorBrush(WindowsUI.Color.FromArgb(a, r, g, b));
      }
  });
#endif
Read more comments on GitHub >

github_iconTop Results From Across the Web

Picker - .NET MAUI
The .NET MAUI Picker displays a short list of items, from which the user can select an item.
Read more >
Setting a Picker's ItemsSource Property - Xamarin
The Picker view is a control for selecting a text item from a list of data. This article explains how to populate a...
Read more >
Setting colours (yes I'm Canadian) does not work on Picker
The inverse problem occurs in dark mode, where setting the TitleColor specifically to Black is ignored and the title will display in its...
Read more >
Picker.Title Property (Microsoft.Maui.Controls)
Gets or sets the title for the Picker. This is a bindable property.
Read more >
Display WinRT UI objects that depend on CoreWindow
Certain pickers, popups, dialogs, and other Windows Runtime (WinRT) objects depend on a CoreWindow; typically to display a user-interface ...
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