modify Binding in code behind

5 posts, 1 answers
  1. Bart
    Bart avatar
    15 posts
    Member since:
    May 2014

    Posted 16 Sep Link to this post

    Hi Guys,

    I'm struggling with this issue for a couple of day's now and I assume this is because I'm new to WPF. but I need some help with the following.

    I am creating a custom planner, and for this I'm using the radschedular like this

    <telerik:RadScheduleView x:Name="MachineplanningScheduleView"
                             ActiveViewDefinitionIndex="{Binding ActiveViewDefinitionIndex, Mode=TwoWay}"                              
                             CurrentDate="{Binding SelectedDate, Mode=TwoWay}"
                             SelectedAppointment="{Binding SelectedAppointment, Mode=TwoWay}"
                             ShowDialog="ShowDialog"
                             AppointmentsSource="{Binding Appointments}"
                             NavigationHeaderVisibility="Collapsed"
                             GroupDescriptionsSource="{Binding GroupDescriptions}"
                             GroupHeaderContentTemplateSelector="{StaticResource GroupHeaderContentTemplateSelector}"
                             ResourceTypesSource="{Binding ResourceTypes}">

    The Binding Appointments works perfect and the calender gets updated when appointments get changed.

    The ShowDialog function in the codebehind should do some database updates and then update the planning without showing the dialog.
    The database updates are going fine, I can cancel the dialog and also I'm able to update the ResourceTypesSource like this

        db.SaveChanges();
     
        RadScheduleView rsv = (RadScheduleView)sender;
        rsv.AppointmentsSource = MachineplanningViewModel.GetCalendarAppointments();
     
        e.Cancel = true;
    }
    the only problem is, while calling "rsv.AppointmentsSource" I lose my binding on Appointments and the calender doesn't get updated anymore when I switch user.

    so my question is, how can I update "Appointments" directly from codebehind so I won't lose my binding.

     

    I hope my explanation is clear and someone can help me.

     

    Thank you.

  2. Yana
    Admin
    Yana avatar
    4554 posts

    Posted 19 Sep Link to this post

    Hi Bart,

    I need to ask you for some additional details on the required approach, so we could try to find a suitable solution. First, why you'd need to set again the AppointmentsSource in ShowDialog event? Can you give us more information on the exact use case you're covering?

    Also, have in mind, that you could use AppointmentCreated, AppointmentEdited and AppointmentDeleted events of RadScheduleView and update the database in their event handlers.

    Looking forward to your reply.

    Regards,
    Yana
    Telerik by Progress
    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
  3. UI for WPF is Visual Studio 2017 Ready
  4. Bart
    Bart avatar
    15 posts
    Member since:
    May 2014

    Posted 19 Sep in reply to Yana Link to this post

    Hi Yana,

    Thank you for the response.
    I the specific situation people are only able to plan a specific appointment for the whole day, so there is no need for a dialog box.
    Single or double clicking with the mouse should add the appointment. According to the algorithm appointments of other persons in the overview can change when I add an appointment. Also the algorithm can decide that my appointment will be 2 days or longer instead of one. This all will be updated in the database and then the appointments are read from the database again.

    If I don't update AppointmentsSource  in the showDialog event added appointments won't appear until I reload the schedular again.
    I'm aware of those events but they are all handled in the code behind, non of them I can bind to another function.

    I hope what I write here makes any sense.

    With Kind Regards

     

    Bart

  5. Bart
    Bart avatar
    15 posts
    Member since:
    May 2014

    Posted 20 Sep Link to this post

    Hi,

    I really hate to tell this but I have found a solution for my issue

    AppointmentsSource="{Binding Appointments, Mode=TwoWay}"

    the thing is, I really needed to use "Mode=TwoWay" here.

    I'm a little bit embarrassed that it took me that long to figure this out.

     

    thanks for your help.

  6. Answer
    Yana
    Admin
    Yana avatar
    4554 posts

    Posted 20 Sep Link to this post

    Hello Bart,

    Thank you for sending the additional information.

    In this case, you could either use the ScheduleView methods for creating/editing appointments which will update the UI as explained in the following topics:
    Create Appointment Using Code
    Edit Appointment Using Code

    Or you could set the AppointmentsSource binding with code, please check the following snippet:

    RadScheduleView rsv = (RadScheduleView)sender;
     
    Binding myBinding = new Binding();
    myBinding.Source = this.DataContext asMachineplanningViewModel;
    myBinding.Path = new PropertyPath("Appointments");
    myBinding.Mode = BindingMode.TwoWay;
     
    BindingOperations.SetBinding(rsv, RadScheduleView.AppointmentsSourceProperty, myBinding);

    I hope this would be helpful.

    Regards,
    Yana
    Telerik by Progress
    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
Back to Top
UI for WPF is Visual Studio 2017 Ready