How to Update Controls when ObservationCollection bound to that controls change

12 posts, 0 answers
  1. Ronak
    Ronak avatar
    108 posts
    Member since:
    Sep 2010

    Posted 21 Apr 2011 Link to this post

    Hi Devs,
    I am new to Silverlight and learning as days goes.Now i have simple Silverlight application which display events from Sharepoint calendars to Rad Scheduler control.
    I am using WCF service to get data from Sharepoint Calendar list and Scheduler is bind to ObservationCollection  of objects. Now if i add,update or delete events in dataSource it does update Control i need to refresh the page.I though it will automatically notify UI to update may be i am wrong can you please advise how ObservationCollection works in binding ?

    Thanks
    Ronak
  2. Rosi
    Admin
    Rosi avatar
    2529 posts

    Posted 25 Apr 2011 Link to this post

    Hello Ronak,

    When you bound the RadScheduler control to an observable collection and this collection change the scheduler will detect the change. This is the expected behavior. That is why I suspect that there is something else in the project that causes the problem. Could you try to isolate the problem in sample project and send it to us so we can test it locally.

    Also, for more details about DataBinding and  RadScheduler you can refer to our online documentation.

    Regards,
    Rosi
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. Ronak
    Ronak avatar
    108 posts
    Member since:
    Sep 2010

    Posted 25 Apr 2011 Link to this post

    Thanks Rosi for your reply.
    as you said that's expected behavior but in case i need to hit F5.would like to send me code ? or just want code for Model and View Model.

    Thanks
    Ronak
  4. Ronak
    Ronak avatar
    108 posts
    Member since:
    Sep 2010

    Posted 26 Apr 2011 Link to this post

    Hi Rosi
    i can not attach code here.File is not of correct type. Try again.
    but here is code for Model and View Model
    Model
    using System;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Ink;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using CAS.Musc.PublicEventsRollup.PublicCalEventsRollupService;
    using System.Collections.ObjectModel;
     
    namespace CAS.Musc.PublicEventsRollup.Model
    {
        public interface IPublicCalEventsServiceProxy  {
            void GetPublicCalEvents(Action<ObservableCollection<PublicEvent>, Exception> callback);
            void GetPublicCalEventsbyUrl(Action<ObservableCollection<PublicEvent>, Exception> callback);
        }
    }
     
     
    using System;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Ink;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using CAS.Musc.PublicEventsRollup.PublicCalEventsRollupService;
    using System.Collections.ObjectModel;
    using System.ServiceModel;
     
    namespace CAS.Musc.PublicEventsRollup.Model
    {
        public class PublicCalEventsServiceProxy :IPublicCalEventsServiceProxy {
            private PublicEventsRollupServiceClient _client;
            //Function to get data from Current Context
            public void GetPublicCalEvents(Action<ObservableCollection<PublicEvent>, Exception> callback) {
     
                 EnsureClient();
                _client.GetAllPublicCalEventsCompleted += (s, e) =>  {
                    if (e.Error != null)  {
                        callback(null, e.Error);
                    }
                    else {
                        callback(e.Result, null);
                    }
                };
                _client.GetAllPublicCalEventsAsync();
            }
            //Function to get data from give Site URL
            public void GetPublicCalEventsbyUrl(Action<ObservableCollection<PublicEvent>, Exception> callback)   {
                EnsureClient();
                _client.GetAllPublicCalEventsByUrlCompleted += (s, e) => {
                    if (e.Error != null)  {
                        callback(null, e.Error);
                    }
                    else  {
                        callback(e.Result, null);
                    }
                };
                _client.GetAllPublicCalEventsByUrlAsync(App.siteURL);
            }
     
            private void EnsureClient()  {
     
                if (_client == null)   {
                    
                    // BasicHttpBinding binding = new BasicHttpBinding();
                   // binding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
                     Uri siteUrl = new Uri(App.Current.Host.Source.GetComponents(System.UriComponents.SchemeAndServer, UriFormat.SafeUnescaped) + "/_vti_bin/_cas_musc/PublicEventsRollupService.svc", UriKind.Absolute);
                    _client = new PublicEventsRollupServiceClient();
                    _client.Endpoint.Address = new EndpointAddress(siteUrl.ToString());
                      
                }
            }
     
        }
    }

    View Model
    using System;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Ink;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using Telerik.Windows.Controls.Scheduler;
    using CAS.Musc.PublicEventsRollup.PublicCalEventsRollupService;
    using Telerik.Windows.Controls;
    namespace CAS.Musc.PublicEventsRollup.Model
    {
        public class AppointmentViewModel : AppointmentBase  {
     
            public AppointmentViewModel(PublicEvent appointment, RadScheduler _scheduler) {
     
                this.UniqueId = appointment.UniqueId;
                this.Subject = appointment.Subject;
                this.Start = appointment.Start;
                this.End = appointment.End;
                this.Body = appointment.Body;
                this.IsAllDayEvent = appointment.IsAllDayEvent;
                this.Location = appointment.Location;
                this.Category = _scheduler.Categories.GetCategoryByName(appointment.Category);
            }
            public AppointmentViewModel()   {
             
            }
            private string _UniqueId;
            public string UniqueId {
                get { return _UniqueId; }
                set {
                    if (_UniqueId != value) {
                        _UniqueId = value;
                        this.OnPropertyChanged(UniqueId);
                    }
                }
            }
            private string _location;
            public string Location {
                get { return this._location; }
                set {
                    if (_location != value) {
                        this._location = value;
                        this.OnPropertyChanged(Location);
                    }
                }
            }
            private string _body;
            public string Body {
                get { return _body; }
                set {
                    if (_body != value) {
                        this._body = value;
                        this.OnPropertyChanged(Body);
                    }
                }
            }
     
            public override IAppointment Copy()  {
                IAppointment appointment = new AppointmentViewModel();
                appointment.CopyFrom(this);
                return appointment;
            }
     
            public override void CopyFrom(IAppointment other)   {
                AppointmentViewModel appointment = other as AppointmentViewModel;
                if (appointment != null)  {
                    base.CopyFrom(other);
                    UniqueId = appointment.UniqueId;
                    Body = appointment.Body;
                    Location = appointment.Location;
                }
     
            }
        }
    }

    MainView Model

    using System;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Ink;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using Telerik.Windows.Controls;
    using CAS.Musc.PublicEventsRollup.Model;
    using System.Collections.ObjectModel;
    using System.ComponentModel;
    using CAS.Musc.PublicEventsRollup.PublicCalEventsRollupService;
    using Telerik.Windows.Controls.Scheduler;
    using Telerik.Windows.Controls.Scheduler.ICalendar;
     
     
     
    namespace CAS.Musc.PublicEventsRollup.ViewModel
    {
        public class MainViewModel : ViewModelBase  {
     
            private IPublicCalEventsServiceProxy _service;
            public RadScheduler Scheduler  {
                get;
                private set;
            }
            public ObservableCollection<AppointmentViewModel> Appointments {
                get;
                private set;
            }
           // public DefaultAppointmentCollection appointments { get; set; }
            private string _errorMessage = string.Empty;
            public string ErrorMessage  {
                get  {
                    return _errorMessage;
                }
                set  {
                    if (_errorMessage == value) {
                        return;
                    }
                    _errorMessage = value;
                    OnPropertyChanged(ErrorMessage);
                }
            }
            private bool _isLoading = false;
            public bool IsLoading {
                get { return _isLoading; }
                set {
                    if (_isLoading == value) { return; }
                    _isLoading = value;
                    OnPropertyChanged("IsLoading");
                }
            }
            private Visibility _isVisible = Visibility.Collapsed;
            public Visibility IsVisible  {
                get { return _isVisible; }
                set {
                        if (_isVisible == value) { return; }
                        _isVisible = value;
                        OnPropertyChanged("IsVisible");
                }
            }
            public MainViewModel() {
     
                if (!DesignerProperties.IsInDesignTool) {
                     var service = new PublicCalEventsServiceProxy();
                    // Events = new ObservableCollection<PublicCalEventViewModel>();
                     Appointments = new ObservableCollection<AppointmentViewModel>();
                    _service = service;
                   // _service.GetPublicCalEvents(HandleResult);
                     _service.GetPublicCalEventsbyUrl(HandleResult);
                    IsLoading = true;
                }
               
            }
            public MainViewModel(RadScheduler _scheduler) {
     
                if (!DesignerProperties.IsInDesignTool)  {
                    
                     Scheduler = _scheduler;
                    var service = new PublicCalEventsServiceProxy();
                    // Events = new ObservableCollection<PublicCalEventViewModel>();
                    Appointments = new ObservableCollection<AppointmentViewModel>();
                    _service = service;
                    // _service.GetPublicCalEvents(HandleResult);
                    _service.GetPublicCalEventsbyUrl(HandleResult);
                    IsLoading = true;
                }
     
            }
            private void HandleResult(ObservableCollection<PublicEvent> result, Exception ex) {
     
                IsLoading = false;
              //  Events.Clear();
                Appointments.Clear();
                if (ex != null) {
                    IsVisible = Visibility.Visible;
                    ErrorMessage = ex.Message;
                    return;
                }
                if (result == null)  {
                    return;
                }
                foreach (var publicCalEvent in result)  {
                   // var vm = new PublicCalEventViewModel(publicCalEvent);
                   // Events.Add(vm);
                    AppointmentViewModel appoinment = new AppointmentViewModel(publicCalEvent, Scheduler);
                   /* appoinment.UniqueId = publicCalEvent.UniqueId;
                    appoinment.Start = publicCalEvent.Start;
                    appoinment.End = publicCalEvent.End;
                    appoinment.Subject = publicCalEvent.Subject;
                    appoinment.Body = publicCalEvent.Body;
                    appoinment.IsAllDayEvent = publicCalEvent.IsAllDayEvent;
                    appoinment.Location = publicCalEvent.Location;
                    appoinment.Category = Scheduler.Categories.GetCategoryByName(publicCalEvent.Category); */
                    /*
                    if (publicCalEvent.RecurrencePattern != null) {
                        RecurrencePattern pattern = new RecurrencePattern();
                        if (RecurrencePatternHelper.TryParseRecurrencePattern(publicCalEvent.RecurrencePattern, out pattern)) {
                            appoinment.RecurrenceRule = new RecurrenceRule(pattern);
                        }
                    } */
                    Appointments.Add(appoinment);
                     
                }
            }
        }
    }


    Thanks
    Ronak
  5. Rosi
    Admin
    Rosi avatar
    2529 posts

    Posted 29 Apr 2011 Link to this post

    Hi Ronak,

    Thank you for the provided code. I reviewed it and didn't notice anything that may causes the problem. However this code it is not runnable and that is why I prepared s sample project that works as expected at our side.You can find it attached to this message. I suggest you modify it so the problem to appear and send it back to us. This will help us a lot in finding the problem and providing you with a solution. You can send zip files by opening a support ticket instead of a forum post.

    Greetings,
    Rosi
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  6. Ronak
    Ronak avatar
    108 posts
    Member since:
    Sep 2010

    Posted 02 May 2011 Link to this post

    Thanks Rosi.
    Well its same thing what i am doing in my case only difference is in my case i have custom appointment class and WCF service to get data from sharepoint.
    tell me how can i submit code using support ticket i mean how i can i transfer this conversation to support ticket.

    Thanks
    Ronak
  7. Ronak
    Ronak avatar
    108 posts
    Member since:
    Sep 2010

    Posted 02 May 2011 Link to this post

    Oh i see now and i don't have any user interface to remove item from collection.
    what i do is manually go to sharepoint list and delete it may be in this case i need to refresh page.

    sorry for confusion.

    Thanks
    Ronak
  8. Ronak
    Ronak avatar
    108 posts
    Member since:
    Sep 2010

    Posted 03 May 2011 Link to this post

    Hey Rosi.
    Today i got complain from users that they need to refresh page eveytime once they add,delete and update events in Target DataSource.
    as it slow to load they dont like to refresh every time they make change.

    Please advise.

    Thanks
    Ronak
  9. Rosi
    Admin
    Rosi avatar
    2529 posts

    Posted 10 May 2011 Link to this post

    Hi Ronak,

    When the observable collection is changed the change will be reflected by the RadScheduleView. That is why you have to take care to update the ViewModel and its properties that are bound to RadScheduleView every time when you update your database records. I suggest you consider updating the ViewModel instead of refreshing the page.

    All the best,
    Rosi
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  10. Ronak
    Ronak avatar
    108 posts
    Member since:
    Sep 2010

    Posted 10 May 2011 Link to this post

    Thanks rosi but sorry about my limited knowledge in silverlight.
    can you please provide any example if u have handy

    Thanks
    Ronak
  11. Ronak
    Ronak avatar
    108 posts
    Member since:
    Sep 2010

    Posted 10 May 2011 Link to this post

    Hi Rosi,I think its not possible in my case because User add,Delete and update items in Sharepoint list so i think they have to refresh page to Query data again.they are not modifying database records through Silverlight UI.

    am i right ? please advise

    Thanks
    Ronak
  12. Rosi
    Admin
    Rosi avatar
    2529 posts

    Posted 13 May 2011 Link to this post

    Hi Ronak,

    Now I understand your requirements. You need to update the collection that is bound to the RadScheduleView when someone updates the database externally.Am I right? In this case this problem is more related to Sharepoint, DataBase and Ria services topics rather than the RadScheduleView. At this stage we are not able to provide you with an example that illustrates this behavior - updating a collection when the database is changed is not a trivial task. It depends on the application settings and requirements.
    That is why I suggest you review the links below that you may find helpful for your scenario:
    http://stackoverflow.com/questions/2939116/datagrid-doesnt-refresh-on-changed-data
    http://stackoverflow.com/questions/1910544/tracking-external-changes-to-a-database-with-linq-to-sql
    http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/ced679d2-de29-4f91-801e-1efe0b4e1c32/
    http://forums.silverlight.net/forums/t/217888.aspx


    Best wishes,
    Rosi
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Back to Top