Category / Section
How to programatically change the selected date in Xamarin.Forms Calendar (SfCalendar)
1 min read
Initially, the calendar is loaded with the current day as selected date and you can initialize the calendar with specific date by using SelectedDate property in SfCalendar.
STEP 1: Create a ViewModel class and add a SelectedDate property with specific date.
public class ViewModel : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; private DateTime selectedDate = DateTime.Now.AddDays(5); public DateTime SelectedDate { get { return this.selectedDate; } set { this.selectedDate = value; this.OnPropertyChanged("SelectedDate"); } } public ViewModel() { } private void OnPropertyChanged(string propertyName) { this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } }
STEP 2: Bind the SelectedDate ViewModel property to the SelectedDate property in SfCalendar.
<calendar:SfCalendar x:Name="calendar" DataSource="{Binding Appointments}" SelectedDate="{Binding SelectedDate}"> <calendar:SfCalendar.BindingContext> <local:ViewModel/> </calendar:SfCalendar.BindingContext> </calendar:SfCalendar>
Output