Hello, I want to bind a collection to a xamChart. My first attempt. Below is the xaml and the code behind and I am not seeing any chart after I run the application. What am I doing wrong?
Thanks
<Grid>
<igCA:XamChart Name="xamChart1" DataContext="{Binding.}">
<igCA:XamChart.Series>
<igCA:Series ChartType="Bar" Fill="#FFD21717" Stroke="#FF000000" DataSource="{Binding Source=MothlyCharges}" Label="Monthly Total Charges" DataMapping="Lable=Month; Value=Amount">
</igCA:Series>
</igCA:XamChart.Series>
</igCA:XamChart>
</Grid>
public partial class MainWindow : Window
{
public MainWindow()
InitializeComponent();
this.DataContext = new MV();
}
public class MV
public MV()
MothlyCharges = new ObservableCollection<MonthlyCharges>();
MothlyCharges.Add(new MonthlyCharges { Month = "Jan 2010", Amount = 213.99m });
MothlyCharges.Add(new MonthlyCharges { Month = "Feb 2010", Amount = 1213.99m });
MothlyCharges.Add(new MonthlyCharges { Month = "Mar 2010", Amount = 213.99m });
MothlyCharges.Add(new MonthlyCharges { Month = "Apr 2010", Amount = 5213.99m });
public ObservableCollection<MonthlyCharges> MothlyCharges
get;
set;
public class MonthlyCharges
public string Month;
public decimal Amount;
I think there is a problem in the value "Amount". The amount is a string, shouldn't it be a float number i.e, remove the letter 'm' from the amount values.
Thanks Peter. For the record, this is my solution for others who are interested to use as an example. I gave the ObservableCollection a different name.
<Window x:Class="NetAdvChart.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" xmlns:igCA="http://infragistics.com/Chart">
<Window.Resources >
</Window.Resources>
<igCA:XamChart Name="xamChart1" DataContext="{Binding .}" >
<igCA:Series ChartType="Column" Fill="Blue" Stroke="#FF000000" DataSource="{Binding AllMonthlyCharges}"
Label="Month"
DataMapping="Label=Month; Value=Amount"></igCA:Series>
</Window>
namespace NetAdvChart
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
AllMonthlyCharges = new ObservableCollection<MonthlyCharges>();
AllMonthlyCharges.Add(new MonthlyCharges { Month = "Jan 2010", Amount = 213.99m });
AllMonthlyCharges.Add(new MonthlyCharges { Month = "Feb 2010", Amount = 1213.99m });
AllMonthlyCharges.Add(new MonthlyCharges { Month = "Mar 2010", Amount = 213.99m });
AllMonthlyCharges.Add(new MonthlyCharges { Month = "Apr 2010", Amount = 5213.99m });
public ObservableCollection<MonthlyCharges> AllMonthlyCharges
public string Month
public decimal Amount
Hello Yassin,
I have been looking into your issue and the code snippet you have provided and can see a few things that could cause this. The first and most important is the DataSource binding you have provided for the Series. By setting the Source property for a binding it is like setting set its DataContext, but you always need to provide a Path. Here is link from the MSDN which should clear things out: http://blogs.msdn.com/b/wpfsdk/archive/2006/10/19/wpf-basic-data-binding-faq.aspx . So in your case, as you have already set the DataContext of the XamChart, you should use:
DataSource="{Binding Path=MothlyCharges}"
Another thing you that should actually fail while compilation is:
I suppose you meant to write DataContext="{Binding}" which is actually the default behavior and is somewhat unnecessary.
Please let me know if you require any further assistance on the matter.
Sincerely,
Petar Monov
Developer Support Engineer
Infragistics Bulgaria
www.infragistics.com/support