I have a silverlight application using telerik charts.
In my view I have the chart in xaml. In the code behind of the view I have something like this:
public partial class MyView : UserControl
{
private MyViewModel viewModel;
public MyView()
{
InitializeComponent();
CreateChartMappings(); // Creates the SeriesMappings for my chart
viewModel = new MyViewModel();
Chart1.ItemsSource = viewModel.MyChartData;
DataContext = viewModel;
Resources.Add("ViewModel", viewModel);
}
}
In my ViewModel I have this:
public class MyViewModel : INotifyPropertyChanged
{
private ObservableCollection<ChartData> myChartData;
public ObservableCollection<ChartData> MyChartData
{
get { return myChartData; }
set { myChartData= value; OnPropertyChanged("MyChartData"); }
}
public MyViewModel()
{
MyWebServiceClient service = MyWebServiceClient.CreateInstance();
service.GetChartDataCompleted +=
new EventHandler<GetChartDataCompletedEventArgs>(GetChartDataCallback);
service.GetChartDataAsync();
}
private void GetChartDataCallback(object sender, GetChartDataCompletedEventArgs e)
{
if (e.Error == null)
{
MyChartData = e.Result;
}
}
}
I know for sure that GetChartData will return the correctly typed data that can be used for the chart and see that GetChartDataCallback does return results in e.Result, but I don't know how to get that data loaded into my chart.
If I do something like Chart1.ItemsSource = viewModel.MyChartData; after I am sure the service returns data then data loads into the chart fine. For example, if I create a button in my view that calls that line of code it will load the data from the service into the chart.
Also if I replace the asyc calls with a normal method call it will work okay as well, so my problem might have something to do with not handling the asyc call correctly?