Hello, I'd like to know if it is possible to bind SelectedDateRange for MCalendar
Hi Josh,
Regarding Query: Hello, I'd like to know if it is possible to bind SelectedDateRange for MCalendar
Yes, you can bind the selected date range value from the ViewModel class. We have prepared the simple sample for achieving your requirement. Please find the sample from the attached link. Also please find the code and screenshot for the same.
Code snippet:
MainPage.Xaml
|
<calendar:SfCalendar x:Name="calendar" SelectionMode="Range" SelectedDateRange="{Binding SelectedDateRange}"> <calendar:SfCalendar.BindingContext> <local:CalendarViewModel/> </calendar:SfCalendar.BindingContext> </calendar:SfCalendar> |
ViewModel.CS
|
public class CalendarViewModel { public CalendarDateRange SelectedDateRange { get; set; } public CalendarViewModel() { SelectedDateRange = new CalendarDateRange(new DateTime(2024, 2, 3, 9, 0, 0), new DateTime(2024, 2, 9, 9, 0, 0)); } } |
Screenshot:
|
|
We hope that this helps you. Please let us know if you need further assistance.
Regards,
Indumathi R
Thanks Indumathi for your reply. One thing I noticed is that when I selected start and end dates, it didn't trigger the binding to the SelectedDateRange in the viewmodel. Do you have any idea?
private CalendarDateRange selectedDateRange;
public CalendarDateRange SelectedDateRange
{
get
{
return selectedDateRange;
}
set
{
if (selectedDateRange != value)
{
selectedDateRange = value;
}
OnPropertyChanged(nameof(SelectedDateRange));
}
}
Hi Josh,
Upon thorough investigation into the reported issue concerning the event not firing for the End Date value, we have identified the underlying cause within the calendar's implementation. It appears that a new instance was only created for the start date of the selected range, while neglecting to instantiate a new instance upon selection of the end date. Consequently, this oversight resulted in the PropertyChanged event not being triggered as expected.
To rectify this issue and ensure proper notification for changes to the end date property, we recommend wiring the PropertyChanged event for the selectedDateRange object. Below, I have provided a code snippet and sample demonstrating this approach:
Code snippet:
|
public CalendarDateRange SelectedDateRange { get { return selectedDateRange; } set { selectedDateRange = value; selectedDateRange.PropertyChanged += SelectedDateRange_PropertyChanged; NotifyPropertyChanged("SelectedDateRange"); } }
private void SelectedDateRange_PropertyChanged(object? sender, PropertyChangedEventArgs e) { //// Todo your requirement here. } |
Screenshot:
|
|
Implementing this adjustment should resolve the issue and ensure that the PropertyChanged event is appropriately triggered for changes to the end date property. Should you encounter any further challenges or require additional assistance, please feel free to reach out.
Regards,
Indumathi R
Indumathi, this implementation only works if the SelectedDateRange is initialized. If that value is not set, then the event is not triggered. Could you please help? Thank you.
Hi Josh,
Thank you for providing the information. Upon reviewing the issue with the SelectedDateRange not triggering without initialization in the ViewModel, we conducted through testing across all platforms (Android, Windows, iOS, Mac) with latest version 24.2.4. After commenting out the SelectedDateRange value initialization code, we observed that the event triggered correctly as expected. We have also verified this behavior with accompanying screenshots for your reference.
In order to further analyze and address the issue promptly, we kindly request the following details:
Reproduction of the issue in the attached sample.
An issue replication video demonstrating the observed behavior.
Screenshot:
Regards,
Indumathi R
Thank you Indumathi. It works n
Hi Josh,
Thank you for the update. We hope issue has been resolved at your end. Please get back to us if you require further other assistance from us.
Regards,
Indumathi R