Articles in this section
Category / Section

How to bind the Selected Dates of Calendar in Xamarin.Forms MVVM ?

3 mins read

 

Calendar supports to select multiple dates through binding the SelectedDates property using the MVVM pattern. To achieve this, follow the below steps.

 

Step 1: First, create a `ViewModel` and add a property for `SelectedDates` in the view model.   

C#

public class CalendarViewModel : INotifyPropertyChanged
{
        private List<DateTime> selectedDates;
        public List<DateTime> SelectedDates
        {
            get { return selectedDates; }
            set
            {
                selectedDates = value;
                RaisePropertyChanged("SelectedDates");
            }
        }
 
        public event PropertyChangedEventHandler PropertyChanged;
 
        public CalendarViewModel()
        {
            SelectedDates = new List<DateTime>();
            SelectedDates.Add(DateTime.Now.Date);
            SelectedDates.Add(DateTime.Now.Date.AddDays(1));
            SelectedDates.Add(DateTime.Now.Date.AddDays(2));
        }
 
        private void RaisePropertyChanged(String Name)
        {
            if (PropertyChanged != null)
                this.PropertyChanged(this, new PropertyChangedEventArgs(Name));
        }
}

 

Step 2: Now, set the `ViewModel` class as the `BindingContext’ for the calendar.

XAML:

<calendar:SfCalendar.BindingContext>
     <local:CalendarViewModel/>
</calendar:SfCalendar.BindingContext>

 

 

Step 3: Now, bind the `SelectedDates` property of `ViewModel` class with Calendar “SelectedDates” property and set SelectionMode as MultiSelection.

C#

<calendar:SfCalendar x:Name="calendar" SelectionMode="MultiSelection" DataSource="{Binding Appointments}" SelectedDates="{Binding SelectedDates, Mode=TwoWay}">
            <calendar:SfCalendar.BindingContext>
                <local:CalendarViewModel/>
            </calendar:SfCalendar.BindingContext>
</calendar:SfCalendar>

 

NOTE

 

If you wish to use the “SelectedDates” property for selecting multiple dates, you must set “SelectionMode” as MultiSelection in Calendar.

 

 

Example: CalendarSelectedDates

 

Conclusion

I hope you enjoyed learning about how to bind the Selected Dates of Calendar in Xamarin.Forms MVVM.

You can refer to our Xamarin.Forms Calendar featuretour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!



Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments
Please sign in to leave a comment
Access denied
Access denied