Articles in this section
Category / Section

How to Get the Current View Dates in the .NET MAUI Calendar (SfCalendar)?

2 mins read

In the Syncfusion .NET MAUI Calendar control, you can get the current view dates of the visible month in the SfCalendar, through the following steps.

 

Step 1: Create a ViewModel class with the property VisibleDates of type List<DateTime>, as shown in the following code sample.

 

C#

public class CalendarViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
 
    private List<DateTime> visibleDates = new List<DateTime>();
 
    public List<DateTime> VisibleDates
    {
        get
        {
            return visibleDates;
        }
        set
        {
            this.visibleDates = value;
            this.OnPropertyChanged(nameof(VisibleDates));
        }
    }
 
    private void OnPropertyChanged(string propertyName)
    {
        this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

 

Step 2: Add the SfCalendar control and the Button control to your layout, as shown in the following code sample.

 

XAML

<ContentPage.Behaviors>
    <local:CalendarBehavior />
</ContentPage.Behaviors>
<ContentPage.Content>
    <Grid RowDefinitions="0.1*, 0.9*">
        <Button x:Name="displayDatesButton"
                HorizontalOptions="Center"
                VerticalOptions="Center"
                Text="Show dates"/>
        <calendar:SfCalendar Grid.Row="1"
                             x:Name="calendar" />
    </Grid>
</ContentPage.Content>

 

Step 3: In the ViewChanged event of the SfCalendar, get the currently visible month’s dates from the NewVisibleDates property of the CalendarViewChangedEventArgs and add them to the VisibleDates property of the ViewModel, as shown in the following code sample.

 

C#

private void Calendar_ViewChanged(object sender, CalendarViewChangedEventArgs e)
{
    if (viewModel == null)
    {
        viewModel = new CalendarViewModel();
    }
 
    if (e.NewView == CalendarView.Month)
    {
        viewModel.VisibleDates.Clear();
        foreach (var item in e.NewVisibleDates)
        {
            if (item.ToString("MMMM") == this.calendar.DisplayDate.ToString("MMMM"))
            {
                viewModel.VisibleDates.Add(item);
            }
 
        }
    }
}

 

Step 4: In the Clicked event of the Button, get the dates of the currently visible month, and display them in a DisplayAlert() dialog using the VisibleDates property of the ViewModel, as shown in the following code sample.

 

C#

private void DisplayDatesButton_Clicked(object sender, EventArgs e)
{
    string dateList = string.Empty;
    foreach (var item in viewModel.VisibleDates)
    {
        dateList += item.ToString("dddd, dd MMMM yyyy") + "\n";
    }
 
    App.Current.MainPage.DisplayAlert(this.calendar.DisplayDate.ToString("MMMM") + " month's dates", dateList, "ok");
}

 

View sample on GitHub

 

Output:

 

Get current view dates in Syncfusion .NET MAUI Calendar

 

Conclusion:

 

Hope you enjoyed learning about how to get the current view dates in the .NET MAUI Calendar (SfCalendar).

You can refer to our .NET MAUI Calendar’s feature tour page to learn about its other groundbreaking feature representations. You can 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 the comments section below. You can also contact us through our support forums, Direct-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 (0)
Please  to leave a comment
Access denied
Access denied