How can I bind the XamWebChart to a SeriesCollection. I'm getting data from a service, in Xml format, containing the values and styles for few series' with several datapoints.
I created SeriesCollection in my ViewModel. Now how should I bind my view to make it use the SeriesCollection to draw graphs.
You will want to check this post to get you started:
https://www.infragistics.com/community/forums/f/ultimate-ui-for-wpf/38169/series-binding-in-xaml-custom-attached-property
Unfortunately there seems to be a problem with the forums this morning, so this post isn't currently accessible.
-Graham
Its back up. So you can check that out if you want. But the exampl linked is really more for if you want to create the series based on a template in Xaml. If you are already creating the series collection in your View Model have you tried just binding it to the chart?
I.E if the property holding the SeriesCollection on your DataContext is called Series. Have you tried
<XamWebChart Series="{Binding Series} ...
Yes, Graham
I tried the same, as
<XamWebChart Series="{Binding ChartSeriesCollection}
where ChartSeriesCollection is a SeriesCollection object in the viewmodel.
And I got XAM_PARSE_EXCEPTOIN at the above XAML Syntax.
The chart doesn't currently tolerate being populated with a null Series collection, so, if using a binding, you have to make sure that what the chart is being bound to can always provide a non-null series collection. For example, if i define my view model as a static resource so that it is accessible at the time of the creation of the page, then it behaves correctly. You could also create a proxy object to bind to that either provided an empty series collection or the actual series collection when accessible. Here is a sample:
The xaml:
<UserControl x:Class="SilverlightApplication60.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480" xmlns:igChart="clr-namespace:Infragistics.Silverlight.Chart;assembly=Infragistics.Silverlight.DataVisualization.Chart.v9.2" xmlns:local="clr-namespace:SilverlightApplication60"> <Grid x:Name="LayoutRoot"> <Grid.Resources> <local:PageViewModel x:Name="testVM" /> </Grid.Resources> <igChart:XamWebChart DataContext="{StaticResource testVM}" Series="{Binding DefinedSeries}"> </igChart:XamWebChart> </Grid> </UserControl>
And the code behind:
public partial class MainPage : UserControl { public MainPage() { InitializeComponent(); } } public class PageViewModel : INotifyPropertyChanged { public PageViewModel() { _definedSeries = new SeriesCollection(); CreateTestData(); } private void CreateTestData() { Series lineSeries = new Series(); lineSeries.ChartType = ChartType.Line; lineSeries.DataMapping = "Label = Label; Value = Value"; lineSeries.DataSource = new TestChartData(); Series columnSeries = new Series(); columnSeries.ChartType = ChartType.Column; columnSeries.DataMapping = "Label = Label; Value = Value"; columnSeries.DataSource = new TestChartData(); _definedSeries.Add(lineSeries); _definedSeries.Add(columnSeries); } private SeriesCollection _definedSeries; public SeriesCollection DefinedSeries { get { return _definedSeries; } set { _definedSeries = value; if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs("DefinedSeries")); } } } public event PropertyChangedEventHandler PropertyChanged; } public class TestChartData : ObservableCollection<TestChartDataItem> { public TestChartData() { Add(new TestChartDataItem { Label = "1", Value = 1 }); Add(new TestChartDataItem { Label = "2", Value = 2 }); Add(new TestChartDataItem { Label = "3", Value = 3 }); } } public class TestChartDataItem { public string Label { get; set; } public double Value { get; set; } }
I've also created a bug (30475) indicating that the chart should tolerate being bound to view model properties that are initially null or unreachable.You can check the status of the bug at any time here or by contacting Developer Support.-Graham
Hi Graham,
Your reply helped. But when I try to update the datacontext of the control later sometime, its not getting updated. Its always showing the initial data.
I have all the property change notifications in place.
In the code, when I get new data I updated the data context as,
ChartControl.DataContext = newVM;
Help me.
Can you provide me the link where I can check the bug status/details? It is very important to us.
Are you sure you are changing the data context at the right level? In the example I posted, if I change the chart's DataContext the series are updated. Are you changing the data context above the element which you originally set it on? Do you have a sample you can provide that exhibits the behavior you are seeing?
The bug is in review, you can check its status at any point by contacting Developer Support and providing them the bug number 30475.
This is my usercontrol,
<UserControl xmlns:my=.........> <UserControl.Resources> <local:ChartViewModel x:Name="chartViewModel"/> </UserControl.Resources> <igChart:XamWebChart DataContext="{StaticResource chartViewModel}" Series="{Binding ChartSeriesCollection}"/> </UserControl>
I have a framework where I need to pick the usercontrols then create and add them to the ui. There I'm creating this control as,
ChartUserControl ucChart = new ChartUserControl(); ChartViewModel vmChart = new ChartViewModel(); vmChart.ChartSeriesCollection = myChartSeriesCollection; ucChart.DataContext = vmChart; regionManager.AddToRegion("MainRegion", ucChart);
The problem I guess is, when I add the control to region, PRISM is creating the control, and setting the datacontext to the one available in the resources of the usercontrol, thus showing the initialization data always.
So how should I reset the datacontext to another one?
Does this problem exists in both DataVisualization and Silverlight XamWebCharts?