How to Select Adjacent Dates Programmatically in the .NET MAUI Calendar (SfCalendar)?
In the Syncfusion® .NET MAUI Calendar control, you can programmatically select adjacent dates by using the Button.Clicked event.
Step 1: Add the SfCalendar and the Button control to your layout, as demonstrated in the following code sample.
XAML
<ContentPage.Behaviors> <local:CalendarBehavior /> </ContentPage.Behaviors> <ContentPage.Content> <Grid RowDefinitions="0.1*, 0.9*" ColumnDefinitions="0.5*, 0.5*"> <Button x:Name="BackwardSelectButton" HorizontalOptions="Center" VerticalOptions="Center" Text="Select Backward" /> <Button Grid.Column="1" x:Name="ForwardSelectButton" HorizontalOptions="Center" VerticalOptions="Center" Text="Select Forward" /> <calendar:SfCalendar Grid.Row="1" Grid.ColumnSpan="2" x:Name="calendar" /> </Grid> </ContentPage.Content>
Step 2: Using the Clicked event of the Button, you can programmatically select the adjacent dates. Additionally, use the SelectionChanged event of the SfCalendar to change the DisplayDate when an adjacent month’s date is selected, as shown in the following code sample.
C#
public class CalendarBehavior : Behavior<ContentPage> { private SfCalendar sfCalendar; private Button backwardButton, forwardButton; protected override void OnAttachedTo(ContentPage bindable) { base.OnAttachedTo(bindable); this.sfCalendar = bindable.FindByName<SfCalendar>("calendar"); if (this.sfCalendar.SelectedDate == null) { this.sfCalendar.SelectedDate = this.sfCalendar.DisplayDate; } this.sfCalendar.SelectionChanged += SfCalendar_SelectionChanged; this.backwardButton = bindable.FindByName<Button>("BackwardSelectButton"); this.backwardButton.Clicked += BackwardButton_Clicked; this.forwardButton = bindable.FindByName<Button>("ForwardSelectButton"); this.forwardButton.Clicked += ForwardButton_Clicked; } private void SfCalendar_SelectionChanged(object sender, CalendarSelectionChangedEventArgs e) { DateTime selectedDate = DateTime.Parse(e.NewValue.ToString()); if (selectedDate.Month != this.sfCalendar.DisplayDate.Month) { sfCalendar.DisplayDate = selectedDate; } } private void BackwardButton_Clicked(object sender, EventArgs e) { if (this.sfCalendar.SelectedDate != null) { this.sfCalendar.SelectedDate = this.sfCalendar.SelectedDate.Value.AddDays(-1); } } private void ForwardButton_Clicked(object sender, EventArgs e) { if (this.sfCalendar.SelectedDate != null) { this.sfCalendar.SelectedDate = this.sfCalendar.SelectedDate.Value.AddDays(1); } } protected override void OnDetachingFrom(ContentPage bindable) { base.OnDetachingFrom(bindable); if (this.sfCalendar != null) { this.sfCalendar.SelectionChanged -= SfCalendar_SelectionChanged; } if (this.backwardButton != null) { this.backwardButton.Clicked -= BackwardButton_Clicked; } if (this.forwardButton != null) { this.forwardButton.Clicked -= ForwardButton_Clicked; } this.sfCalendar = null; this.backwardButton = null; this.forwardButton = null; } }
Download the complete sample on GitHub
Output
Conclusion
I hope you enjoyed learning how to programmatically select the adjacent dates 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!