Hi,
I have attached a simple Project, where I would like to display two data series in one chart. It works if I create the DataContext in the Code Behind file (MainWindow.xaml.cs). But I want to set the DataContext for the XamDataChart in the xaml file (MainWindow.xaml). For this I added
<ig:XamDataChart.DataContext> <local:ChartViewModel/> </ig:XamDataChart.DataContext>
But I doesn't work. I'm sure there is a simple solution, but I can't figure it out.
Thanks for your help.
Thomas
I also try to transfer the IG example
"(NetAdvantage 2012.2, Samples Browser) -> xamDataChart -> Performance -> Updating Data Points"
into my Project. But I it crashes, because the pointers of "m_ChartViewModels" are null.
I'm very confused.
Hello Thomas,
Thank you for your post. I have been looking into the sample applications that you have attached and it seems that the reason for the data to not be displayed in the XamDataChart, when you set the DataContext through xaml, is that the two collections that you are using as source for the series does not have items in them. When you create an instance of your view model through xaml, the constructor without parameters is used and since you are not adding data in the two collections, in that constructor, no data is displayed. I have modified the sample application that you have attached, in order to show how you can initialize your data in the constructor of your view model.
Regarding the second sample application that you have attached, it seems that the reason for the NullReferenceException that you are getting is that you are trying to access resources before their initialization. Since you are getting the two ChartViewModels defined in the Resources of the MainWindow, before the InitializeComponents method to be called, the Resources collection returns null for both objects. The “DataSource1” and “DataSource2” objects and all of the other elements of the MainWindow, are initialized in the InitializeComponents method and if you wish to access one of those elements, you can do that in after the InitializeComponents is called.
Please let me know if you need any further assistance on the matter.
Sincerely,
Krasimir
Developer Support Engineer
Infragistics
www.infragistics.com/support
Hi Krasimir,
thank you for your response.
1.
Your solution fo my first project shows me, that ChartViewModel do all thing by its own. But I want to set the data otherwhere.
But I still have access to the DataContext of ChartViewModel via the code behind in this way:
ChartViewModel cvm = this.DataChart.DataContext as ChartViewModel;
And the I can change/add the data.
2.
But I prefer my second project, because I like that I hold the instances of ChartViewModel in a resource.
And you are right. I moved the call of "InitializeComponent();" at the top of "MainWindow()" and the instances are set. Furthermore I changed the "Binding" in the ScatterLineSeries from
ItemsSource="{Binding Data1}" to ItemsSource="{Binding Data}"
resp.
ItemsSource="{Binding Data2}" to ItemsSource="{Binding Data}"
And it works fine.
Thank you again,
Thank you for your reply. I am very glad that you have managed to solve the issues that you were having. Please let me know if you need any further assistance on the matter.