When using a Picker control in UWP you get an unwanted margin on top of the picker.
<StackLayout>
<Label Text="Welcome to Xamarin.Forms!" BackgroundColor="Yellow"/>
<Picker BackgroundColor="Green"/>
</StackLayout>
The picker should en up right under the label (it does in xamarin forms 4.2.x)
You get a margin (about as high as the picker itself) between the picker and the label
Confirmed.
_8150 Repro.zip
Seems the Header will always be rendered on UWP even though no Picker Title property is filled in... I submitted PR #8160 to amend this.
If anyone needs a workaround while waiting for the fix, add the following custom picker renderer in your UWP project:
using AppShack.UWP.Renderers;
using Xamarin.Forms;
using Xamarin.Forms.Internals;
using Xamarin.Forms.Platform.UWP;
[assembly: ExportRenderer(typeof(Picker), typeof(CustomPickerRenderer))]
namespace AppShack.UWP.Renderers
{
[Preserve(AllMembers = true)]
public class CustomPickerRenderer : PickerRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs
{
base.OnElementChanged(e);
if (Control != null)
Control.Header = null;
}
}
}
I have this on my project so that the title displays as the placeholder text instead, which is more consistent to what other platforms do in Xamarin.forms
protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
{
base.OnElementChanged(e);
if (Control != null)
{
if (!string.IsNullOrWhiteSpace(Element?.Title))
{
Control.Header = null;
Control.PlaceholderText = Element.Title;
}
}
}
I guess I'll need a variation to include this newer workaround.
Your workaround doesn't fix anything for me @jimmccurdy and the workaround from @MitchBomcanhao breaks picker completely.
@MagicAndre1981 I made another workaround in my code. You can try replace
Control.Header = null;
with
if (Control.Header is UIElement tb)
{
tb.Visibility = Visibility.Collapsed;
}
ok, with some code of the linked PR it works fine for my case:
protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
{
base.OnElementChanged(e);
if (Control != null)
{
Control.Header = null;
Control.HeaderTemplate = string.IsNullOrEmpty(Element.Title) ? null : (Windows.UI.Xaml.DataTemplate)Windows.UI.Xaml.Application.Current.Resources["ComboBoxHeader"];
Control.DataContext = Element;
}
}
I'll use this now until PR gets merged and released in next 4.3 version.
_// Edit, this causes that I can't have empty picker so it is always one entry selected, so not that good as I first thought._
I submitted PR #8160 to amend this.
Hi, @Depechie can you respond to the change requests so that it can be merged?
Talked to Gerald about it. Normally end of this week I will have some time to test the PR again to see if everything is ok to merge it.
ok, thanks
Is the issue still present in 4.4? Looks like @Depechie was not able to make the changes in his PR.
Yes, still present in 4.4.0991265 :(
Any info about when this will be fixed?
@jimmccurdy's workaround didn't work for me, but got me on the right track.
For me, I had to use the line
Control.HeaderTemplate = new Windows.UI.Xaml.DataTemplate();
instead of
Control.Header = null;
Hope this helps, and thanks to @jimmccurdy for the original workaround :)
Most helpful comment
If anyone needs a workaround while waiting for the fix, add the following custom picker renderer in your UWP project:
using AppShack.UWP.Renderers;
using Xamarin.Forms;
using Xamarin.Forms.Internals;
using Xamarin.Forms.Platform.UWP;
[assembly: ExportRenderer(typeof(Picker), typeof(CustomPickerRenderer))] e)
namespace AppShack.UWP.Renderers
{
[Preserve(AllMembers = true)]
public class CustomPickerRenderer : PickerRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs
{
base.OnElementChanged(e);
if (Control != null)
Control.Header = null;
}
}
}