How to change the selected date programmatically in the .NET MAUI Calendar (SfCalendar)?
In the Syncfusion® .NET MAUI Calendar control, you can programmatically change the selected date of the SfCalendar.
Step 1: Add the Button control and SfCalendar control to your layout, as shown in the following code sample.
XAML
<ContentPage.Behaviors> <local:CalendarBehavior /> </ContentPage.Behaviors> <Grid> <Grid.RowDefinitions> <RowDefinition Height="0.9*" /> <RowDefinition Height="0.1*" /> </Grid.RowDefinitions> <calendar:SfCalendar x:Name="calendar" /> <Button Grid.Row="1" x:Name="button" VerticalOptions="Center" HorizontalOptions="Center" Text="Click here to change selected date" /> </Grid>
Step 2: Use the Clicked event of the Button to change the selected date programmatically, as demonstrated in the following C# code sample:
C#
public class CalendarBehavior : Behavior<ContentPage> { private SfCalendar calendar; private Button buttton; protected override void OnAttachedTo(ContentPage bindable) { base.OnAttachedTo(bindable); this.calendar = bindable.FindByName<SfCalendar>("calendar"); this.buttton = bindable.FindByName<Button>("button"); this.buttton.Clicked += Buttton_Clicked; } private void Buttton_Clicked(object sender, EventArgs e) { this.calendar.SelectedDate = this.calendar.DisplayDate.AddDays(5); } protected override void OnDetachingFrom(ContentPage bindable) { base.OnDetachingFrom(bindable); if (this.buttton != null) { this.buttton.Clicked -= Buttton_Clicked; } this.buttton = null; this.calendar = null; } }
Download the complete sample on GitHub
Output:
Conclusion
I hope you enjoyed learning how to change the selected date programmatically in the .NET MAUI Calendar (SfCalendar).
You can refer to our .NET MAUI Calendar’s feature tour page to know about its other groundbreaking feature representations. Explore our .NET MAUI Calendar documentation to understand how to present and manipulate data.
For current customers, you can check out our .NET MAUI 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 .NET MAUI Calendar and other .NET MAUI components.
If you have any queries or require clarifications, please let us know in comments below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!