Skip to content

Commit bec36bc

Browse files
996026 - UG Documentation for the Special Date Predicate On Demand on the SfCalendar
1 parent 6a55b58 commit bec36bc

File tree

2 files changed

+78
-1
lines changed

2 files changed

+78
-1
lines changed

MAUI/Calendar/customizations.md

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,87 @@ this.calendar.MonthView.SpecialDayPredicate = (date) =>
160160
{% endtabs %}
161161

162162
![Special day icon in .NET MAUI Calendar.](images/customization/net-maui-special_day_predicate.png)
163-
164163
>**NOTE**
165164
* The Background color and text style will be applied based on the following order: selectableDayPredicate dates, special dates, disable dates, today date, weekend dates, trailingLeading dates, and normal dates.
166165

166+
* **Special day predicate On Demand** - The on-demand refresh feature allows the `SfCalendar` to dynamically update special dates after new data is fetched. You can explicitly trigger a refresh to re-invoke the [SpecialDayPredicate](https://help.syncfusion.com/cr/maui/Syncfusion.Maui.Calendar.CalendarMonthView.html#Syncfusion_Maui_Calendar_CalendarMonthView_SpecialDayPredicate), ensuring that visual indicators such as icons, text styles, and backgrounds reflect the latest metadata only after the update. The new [UpdateSpecialDayPredicate]https://help.syncfusion.com/cr/maui/Syncfusion.Maui.Calendar.CalendarMonthView.html#Syncfusion_Maui_Calendar_UpdateSpecialDayPredicate) provides a direct way to force this re-evaluation, guaranteeing that special-day indicators display the most recent data after a refresh.
167+
168+
{% tabs %}
169+
{% highlight xaml tabtitle="MainPage.xaml" %}
170+
171+
<calendar:SfCalendar x:Name="calendar" View="Month">
172+
</calendar:SfCalendar>
173+
174+
{% endhighlight %}
175+
{% highlight c# tabtitle="MainPage.xaml.cs" %}
176+
177+
public class MainPage : ContentPage
178+
{
179+
180+
public List<DateTime> SpecialDatesCollection = new List<DateTime>();
181+
182+
public MainPage()
183+
{
184+
InitializeComponent();
185+
calendar.ViewChanged += Calendar_ViewChanged;
186+
this.calendar.MonthView.SpecialDayPredicate = (date) =>
187+
{
188+
foreach (DateTime dates in SpecialDatesCollection)
189+
{
190+
if (date.Date == dates.Date)
191+
{
192+
CalendarIconDetails calendarIcon = GetSpecialDates(dates);
193+
return calendarIcon;
194+
}
195+
}
196+
return null;
197+
};
198+
}
199+
200+
private CalendarIconDetails GetSpecialDates(DateTime date)
201+
{
202+
if (SpecialDatesCollection.Contains(date.Date))
203+
{
204+
CalendarIconDetails calendarIconDetails=new CalendarIconDetails()
205+
{
206+
Icon = CalendarIcon.Diamond,
207+
Fill = Colors.Red,
208+
};
209+
return calendarIconDetails;
210+
}
211+
return null;
212+
}
213+
214+
private async void Calendar_ViewChanged(object sender, CalendarViewChangedEventArgs e)
215+
{
216+
SpecialDatesCollection.Clear();
217+
await LoadSpecialDatesFromAPIAsync(e.NewVisibleDates);
218+
calendar.UpdateSpecialDayPredicate();
219+
}
220+
221+
private async Task LoadSpecialDatesFromAPIAsync(CalendarDateRange range)
222+
{
223+
var httpClient = new HttpClient();
224+
var requestData = new { StartDate = range.StartDate.Date, EndDate = range.EndDate.Date };
225+
var response = await httpClient.PostAsJsonAsync("https://your-api.com/special-dates", requestData);
226+
response.EnsureSuccessStatusCode();
227+
var apiResponse = await response.Content.ReadFromJsonAsync<ApiSpecialDatesResponse>();
228+
if (apiResponse?.SpecialDates != null)
229+
{
230+
foreach (var dateStr in apiResponse.SpecialDates)
231+
{
232+
if (DateTime.TryParse(dateStr, out var date))
233+
specialDates.Add(date.Date);
234+
}
235+
}
236+
}
237+
}
238+
239+
{% endhighlight %}
240+
{% endtabs %}
241+
242+
![Special day iOn Demand con in .NET MAUI Calendar.](images/customization/net-maui-special_day_predicate_ondemand.png)
243+
167244
## Year cell customization
168245
You can customize the calendar `year`, `decade`, and `century` views by using the `YearView` property of `SfCalendar`.
169246

24.8 KB
Loading

0 commit comments

Comments
 (0)