logeeks
logeeks

Reputation: 4979

ObservableCollection not updating the control

I am having a Telerik TransitionControl which displays advertisements to to end user. the logic is written in such a way that the ad images will be downloaded asynchronously in the behind. the control will display images as it is available. I am using ObservableCollection to hold the advertisement images.New image information is added to this ObservableCollection when a image is successfully downloaded. However, the Telerik TransitionControl is not getting updated with the new images.

I believe the ObservableCollection does not need the OnNotifyPropertyChanged to be called as it will be called internally

Code is given below

//Inside the AdvertUserControl.xaml.cs

ViewModel vm = new ViewModel();
DataContext = vm;

this.radControl.SetValue(AdRotatorExtensions.AdRotatorExtensions.ItemsSourceProperty, vm.SquareAdsVertical);

//Inside the ViewModel.cs

 public ReadOnlyObservableCollection<Advert> SquareAdsVertical
        {
            get
            {
                if (AdsManager.VerticalAds == null)
                {

                    return null;
                }
                return new ReadOnlyObservableCollection<Advert>(AdsManager.VerticalAds);
            }
        }


// Inside DownloadManager.cs
   private static ObservableCollection<Advert> adsToShowVertical = new ObservableCollection<Advert>();
public static ObservableCollection<Advert> VerticalAds
        {
            get { if (adsToShowVertical != null) return adsToShowVertical;
                return null;
            }
        }

 public static void OnDownloadComplete(Object sender, AsyncCompletedEventArgs e)
{
        try
        {


    if(!e.Cancelled)
    {

        if (e.Error == null)
        {
            Advert ad = e.UserState as Advert ;
         adsToShowVertical.Add(ad  );
        }

}

Upvotes: 1

Views: 212

Answers (2)

Pascal Pich&#233;
Pascal Pich&#233;

Reputation: 610

You need to return only one instance of the read only collection created from your observable collection. If you change a value in the Observable list, your control will be refreshed through the readonly collection.

Upvotes: 0

Chris Taylor
Chris Taylor

Reputation: 53719

I have not used the Telerik controls, but I suspect that if you change the following code in your View Model

public ReadOnlyObservableCollection<Advert> SquareAdsVertical 
{ 
    get 
    { 
        if (AdsManager.VerticalAds == null) 
        { 
            return null; 
        } 
        return new ReadOnlyObservableCollection<Advert>(AdsManager.VerticalAds); 
    } 
} 

To the following

private ReadOnlyObservableCollection<Advert> _readonlyAds;
public ReadOnlyObservableCollection<Advert> SquareAdsVertical 
{ 
    get 
    { 
        if (AdsManager.VerticalAds == null) 
        { 
            return null; 
        } 
        else if (_readonlyAds == null)
        {
            // Only one instance of the readonly collection is created
            _readonlyAds = new ReadOnlyObservableCollection<Advert>(AdsManager.VerticalAds);
        }

        // Return the read only collection that wraps the underlying ObservableCollection
        return  _readonlyAds;
    } 
} 

Upvotes: 1

Related Questions