Skip to content

Commit 4149f1e

Browse files
Merge pull request #3831 from syncfusion-content/996026-SpecialDateOnDemand
996026 - UG Documentation for the Special Date Predicate On Demand on SfCalendar
2 parents 4dd302d + 7dc2e1c commit 4149f1e

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

MAUI/Calendar/customizations.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,82 @@ this.calendar.MonthView.SpecialDayPredicate = (date) =>
164164
>**NOTE**
165165
* 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.
166166

167+
* **UpdateSpecialDayPredicate** - The UpdateSpecialDayPredicate 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 reevaluation, guaranteeing that special day indicators display the most recent data after a refresh.
168+
169+
{% tabs %}
170+
{% highlight xaml tabtitle="MainPage.xaml" %}
171+
172+
<calendar:SfCalendar x:Name="calendar" View="Month">
173+
</calendar:SfCalendar>
174+
175+
{% endhighlight %}
176+
{% highlight c# tabtitle="MainPage.xaml.cs" %}
177+
178+
public class MainPage : ContentPage
179+
{
180+
181+
public List<DateTime> SpecialDatesCollection = new List<DateTime>();
182+
183+
public MainPage()
184+
{
185+
InitializeComponent();
186+
calendar.ViewChanged += Calendar_ViewChanged;
187+
this.calendar.MonthView.SpecialDayPredicate = (date) =>
188+
{
189+
foreach (DateTime dates in SpecialDatesCollection)
190+
{
191+
if (date.Date == dates.Date)
192+
{
193+
CalendarIconDetails calendarIcon = GetSpecialDates(dates);
194+
return calendarIcon;
195+
}
196+
}
197+
return null;
198+
};
199+
}
200+
201+
private CalendarIconDetails GetSpecialDates(DateTime date)
202+
{
203+
if (SpecialDatesCollection.Contains(date.Date))
204+
{
205+
CalendarIconDetails calendarIconDetails=new CalendarIconDetails()
206+
{
207+
Icon = CalendarIcon.Diamond,
208+
Fill = Colors.Red,
209+
};
210+
return calendarIconDetails;
211+
}
212+
return null;
213+
}
214+
215+
private async void Calendar_ViewChanged(object sender, CalendarViewChangedEventArgs e)
216+
{
217+
SpecialDatesCollection.Clear();
218+
await LoadSpecialDatesFromAPIAsync(e.NewVisibleDates);
219+
calendar.UpdateSpecialDayPredicate();
220+
}
221+
222+
private async Task LoadSpecialDatesFromAPIAsync(CalendarDateRange range)
223+
{
224+
var httpClient = new HttpClient();
225+
var requestData = new { StartDate = range.StartDate.Date, EndDate = range.EndDate.Date };
226+
var response = await httpClient.PostAsJsonAsync("https://your-api.com/special-dates", requestData);
227+
response.EnsureSuccessStatusCode();
228+
var apiResponse = await response.Content.ReadFromJsonAsync<ApiSpecialDatesResponse>();
229+
if (apiResponse?.SpecialDates != null)
230+
{
231+
foreach (var dateStr in apiResponse.SpecialDates)
232+
{
233+
if (DateTime.TryParse(dateStr, out var date))
234+
specialDates.Add(date.Date);
235+
}
236+
}
237+
}
238+
}
239+
240+
{% endhighlight %}
241+
{% endtabs %}
242+
167243
## Year cell customization
168244
You can customize the calendar `year`, `decade`, and `century` views by using the `YearView` property of `SfCalendar`.
169245

0 commit comments

Comments
 (0)