Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add minimum date validation check for DatePicker in Xamarin

I am having two DatePicker (named fromdate and todate) and a button (named as save).

By default, today's date will be populated in both the DatePicker. And I have added minimum date validation ( the user can select only today's or greater date.

If today's date is 30th May and the user selects 15th June from fromdate and clicked ok. Now the default today date is displayed in todate. I want a validation like if the user selects 15th June in fromdate and click ok, then the todate should be populated with fromdate.

like image 522
Navin Samuel Avatar asked Jul 15 '17 19:07

Navin Samuel


1 Answers

This can be done by binding the MinimumDate of the toDate DatePicker to the FromDate property.

I don't know your code so I will give you an example:

Imagine you have a XAML like this:

<?xml version="1.0" encoding="utf-8"?>
<ContentPage 
    xmlns="http://xamarin.com/schemas/2014/forms" 
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
    xmlns:local="clr-YourProject" 
    x:Class="YourProject.YourClass">

    <StackLayout>
        <DatePicker Date="{Binding FromDate}" MinimumDate="{Binding FromMiminumDate}" />

        <DatePicker Date="{Binding ToDate}" MinimumDate="{Binding FromDate}" />
    </StackLayout>
</ContentPage>

And you have a ViewModel like this:

public class YourViewModel : INotifyPropertyChanged
{
    public YourViewModel ()
    {
        FromMiminumDate = DateTime.Today;
    }

    private DateTime _fromDate;
    public DateTime FromDate
    {
        get { return _fromDate; }
        set
        {
            if (_fromDate == value)
                return;

            _fromDate = value;
            NotifyPropertyChanged (nameof(FromDate));
        }
    }

    private DateTime _toDate;
    public DateTime ToDate
    {
        get { return _toDate; }
        set
        {
            if (_toDate == value)
                return;

            _toDate = value;
            NotifyPropertyChanged (nameof(ToDate));
        }
    }

    private DateTime _fromMiminumDate;
    public DateTime FromMiminumDate
    {
        get { return _fromMiminumDate; }
        set
        {
            if (_fromMiminumDate == value)
                return;

            _fromMiminumDate = value;
            NotifyPropertyChanged (nameof(FromMiminumDate));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    void NotifyPropertyChanged (string propertyName)
    {
        PropertyChanged?.Invoke (this, new PropertyChangedEventArgs (propertyName));
    }
}

This will make that every time you change the date selected in the FromDate Picker the MinimumDate for the ToDate Picker will be changed too.

And of course the XAML code behind will be as simple as:

public YourClass ()
{
    var VM = new YourViewModel ();

    InitializeComponent ();

    BindingContext = VM;
}

Hope this helps.

like image 186
pinedax Avatar answered Sep 24 '22 09:09

pinedax