Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin Forms: Date picker not resizing full width on change

I have added a date picker in my Xamarin forms page. The issue I'm facing is on initial page load the size that the date picker occupies in the screen doesn't get resized on changing the date, if I select a date where text length is more than what was selected before.

Say for Example, If I had selected the date 21-May-2018 initially and If I change the date to 21-November-2018, now since the text length has increased the full text is not shown on the screen.

Please let me know how can I fix this issue.

XAML:

<StackLayout Style="{StaticResource layoutListItem}">
                            <Label Style="{StaticResource labelStandard}" Text="From" HorizontalOptions="Start" />
                            <DatePicker Date="{Binding FromDateFilter}" TextColor="#777777" VerticalOptions="CenterAndExpand" 
                                        HorizontalOptions="EndAndExpand" MaximumDate="{Binding ToDateFilter}">
                                <DatePicker.Format>dd-MMMM-yyyy</DatePicker.Format>
                                <DatePicker.Effects>
                                    <effects:EntryNoBorderEffect />
                                </DatePicker.Effects>
                            </DatePicker>
                        </StackLayout>

Initial Load: (Date Range -> From : 29-may-2017)

enter image description here

On change: (Date Range -> From: 29-november-2017)

enter image description here

like image 736
Aryan M Avatar asked Nov 29 '18 10:11

Aryan M


1 Answers

For those who are still waiting for a fix even on version Xamarin.Forms 5.0.0.2012, here's a workaround:

Extend Xamarin.Forms.DatePicker and call InvalidateMeasure whenever the date changes:

public class FixedDatePicker : DatePicker
{
    public FixedDatePicker()
    {
        DateSelected += FixedDatePicker_DateSelected;
    }

    private void FixedDatePicker_DateSelected(object sender, DateChangedEventArgs e)
    {
        InvalidateMeasure();
    }
}

Use this extended class instead of the default in your XAML.

I hope that the Xamarin team fixes this bug soon.

like image 128
Amir Mahdi Nassiri Avatar answered Oct 18 '22 07:10

Amir Mahdi Nassiri