How to bind SelectedDateRange for mvvm

Hello, I'd like to know if it is possible to bind SelectedDateRange for MCalendar


7 Replies 1 reply marked as answer

IR Indumathi Ravichandran Syncfusion Team February 7, 2024 05:51 AM UTC

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



JP Josh Pham February 7, 2024 04:24 PM UTC

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));

    }

}



IR Indumathi Ravichandran Syncfusion Team February 8, 2024 06:39 AM UTC

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


Attachment: RangeCalendar_b14df075.zip


JP Josh Pham February 13, 2024 05:17 PM UTC

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.



IR Indumathi Ravichandran Syncfusion Team February 14, 2024 10:41 AM UTC

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


Attachment: RangeCalendar_92869f69.zip

Marked as answer

JP Josh Pham February 14, 2024 03:50 PM UTC

Thank you Indumathi. It works n 



IR Indumathi Ravichandran Syncfusion Team February 16, 2024 05:03 AM UTC

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


Loader.
Up arrow icon