Command Alternative for DatePicker

4 posts, 0 answers
  1. Scott
    Scott avatar
    38 posts
    Member since:
    Sep 2012

    Posted 29 Apr 2015 Link to this post

    I'm using a datepicker in my silverlight application.  The SelectedDate is bound to a property in my ViewModel (I'm using MVVM).  I noticed there is no command for DatePicker.  The DatePicker is used to select a start date to view a schedule.  I have a method in my ViewModel that refreshes the user's schedule, and when a new date is picked I want the schedule to refresh with the newly selected date.  I would prefer a command, but I am trying to use SelectionChanged since there's no command availabled.  I call this RefreshSchedule method in the SelectionChanged event, but the problem is this is firing every time SelectionChanged is fired, including when the data is initially bound.  Is there any way I can only call this method when a USER changes the selection, not just any time the bound data is loaded/changed?  I feel like I'm missing something obvious here.
  2. Kalin
    Admin
    Kalin avatar
    1208 posts

    Posted 04 May 2015 Link to this post

    Hello Scott,

    You can use EventToCommandBehavior in order to execute a custom command in the ViewModel whenever the SelectionChanged event has occurred. Check the following code snippet:

    <telerik:RadDatePicker SelectedValue="{Binding SelectedDate}">
        <telerik:EventToCommandBehavior.EventBindings>
            <telerik:EventBinding Command="{Binding CustomCommand}"
                            EventName="SelectionChanged"
                            PassEventArgsToCommand="True" />
        </telerik:EventToCommandBehavior.EventBindings>
    </telerik:RadDatePicker>

    I tested it and the command was not executed with the initial set of SelectedValue, so this should help you to achieve the required.

    Hope this will work for you.

    Regards,
    Kalin
    Telerik
     

    See What's Next in App Development. Register for TelerikNEXT.

     
  3. DevCraft banner
  4. Scott
    Scott avatar
    38 posts
    Member since:
    Sep 2012

    Posted 04 May 2015 in reply to Kalin Link to this post

    I tried using EventToCommandBehavior as described in your post, but unlike your results the command is being fired on the setting of SelectedValue for me.  I have code that populates SelectedValue, and when that binding occurs the command is being fired (I dont want it to).  This causes an unnecessary refreshing of my user's schedules.  I still need to be able to discern between code updating the value and the user clicking on the calendar and selecting a value.  I only want to call this update method when the user selected a new date.
  5. Kalin
    Admin
    Kalin avatar
    1208 posts

    Posted 07 May 2015 Link to this post

    Hello Scott,

    Please check the attached sample project and let me know how it works for you. What I can also suggest you would be to implement a property (flag) that indicates whether your are updating the SelectedValue, so you can skip calling the ScheduleView update method.

    Hope this helps.

    Regards,
    Kalin
    Telerik
     

    See What's Next in App Development. Register for TelerikNEXT.

     
Back to Top