This is a migrated thread and some comments may be shown as answers.

Update Radchart Seriesmappings and Itemsource using MVVM Backgroundworker

1 Answer 86 Views
Chart
This is a migrated thread and some comments may be shown as answers.
hasan
Top achievements
Rank 1
hasan asked on 25 Sep 2010, 06:17 PM

Hello,

I have a WPF application which incorporates the MVVM pattern. I have a viewmodel class which basically has two properties:

01.private SeriesMappingCollection _mapCollection;
02.private ArrayList _joints;

03.public SeriesMappingCollection MapCollection
04.{
05.    get { return _mapCollection; }
06.    set
07.    {
08.         if (_mapCollection != value)
09.         {
10.              _mapCollection = value;
11.              OnPropertyChanged("MapCollection");
12.         }              
13.     }
14.}      
15.  
16.public ArrayList Joints
17.{
18.     get { return _joints; }
19.     set
20.     {
21.           if (_joints != value)
22.           {
23.               _joints = value;
24.               OnPropertyChanged("Joints");
25.           }
26.      
27.}


The values of Mapcollection and Joints are set using a BackgroundWorker. When the data has been fetched, the GetAlertCompleted function is fired and the Mapcollection and Joints properties are set. This consequently fires the OnPropertyChanged() function of the view model 

01.public ViewModel()
02.{
03.       workerAlerts = new BackgroundWorker();
04.       workerAlerts.DoWork += (o, args) => args.Result = 
05.           VizBackend.GetAlert.(_ipAdd,_selectedPeriod);
06.              
07.       workerAlerts.RunWorkerCompleted += GetAlertCompleted;
08.}
09.  
10.private void GetAlertCompleted(object sender, RunWorkerCompletedEventArgs e)
11.{
12.  
13.     var alertData = e.Result as AlertData;
14.     Joints = alertData.Joints;
15.     MapCollection = alertData.MapCollection;
16. }

And in the main window I have:

01.private readonly ViewModel vm;
02.vm = (ViewModel)DataContext;
03.vm.PropertyChanged += VmPropertyChanged;
04.  
05.private void VmPropertyChanged(object sender, PropertyChangedEventArgs e)
06.        {
07.            switch (e.PropertyName)
08.            {
09.                case "MapCollection":
10.                    this.radChart.SeriesMappings = vm.MapCollection;
11.                    break;
12.  
13.                case "Joints":
14.                    this.radChart.ItemsSource = vm.Joints;
15.                    break;
16.            }
17.        }
The problem is that when I assign the first property of the RadChart ( for example ItemsSource), I cant set the SeriesMappings property and I receive this error "The calling thread cannot access this object because a different thread owns it". I have no idea why this error is being raised. If I can set one property, why cant I set the second one. And it also doesnt matter in which order they are assigned. Once the first is assigned, I get an error for the second one. 


I would appreciate if you could point out where the problem might be. Thanks.


1 Answer, 1 is accepted

Sort by
0
Yavor
Telerik team
answered on 29 Sep 2010, 09:36 AM
Hi hasan,

Thank you for contacting Telerik support!

Unfortunately you can't define the series mappings in a thread different than the UI thread. This is because of a WPF / Silverlight restriction that doesn't allow DependencyObject to be created in a background thread. Since SeriesMapping derives from DependencyObject this is the cause of the exception you get. I am sorry for the caused inconvenience!

All the best,
Yavor Ivanov
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
Tags
Chart
Asked by
hasan
Top achievements
Rank 1
Answers by
Yavor
Telerik team
Share this question
or