FlexChart > Quick Start > Step 2: Binding FlexChart to a Data Source |
This step allows you to bind FlexChart to a data source.
class DataCreator { public static List<DataItem> CreateData() { var data = new List<DataItem>(); data.Add(new DataItem("UK", 5, 4)); data.Add(new DataItem("USA", 7, 3)); data.Add(new DataItem("Germany", 8, 5)); data.Add(new DataItem("Japan", 12, 8)); return data; } } public class DataItem { public DataItem(string country, int sales, int expenses) { Country = country; Sales = sales; Expenses = expenses; } public string Country { get; set; } public int Sales { get; set; } public int Expenses { get; set; } }
<Grid> <c1:C1FlexChart x:Name="flexChart" BindingX="Country" ItemsSource="{Binding DataContext.Data}" Rendered="flexChart_Rendered"> <c1:C1FlexChart.Series> <c1:Series SeriesName="Sales" Binding="Sales"/> <c1:Series SeriesName="Expenses" Binding="Expenses"/> </c1:C1FlexChart.Series>
To specify the binding source, you need to add the DataContext = "{Binding RelativeSource={RelativeSource Mode=Self}}" markup in the <Window> tag of the MainWindow.xaml file. |
Private _data As List(Of DataItem) Public Sub New() Me.InitializeComponent() End Sub Public ReadOnly Property Data() As List(Of DataItem) Get If _data Is Nothing Then _data = DataCreator.CreateData() End If Return _data End Get End Property
private List<DataItem> _data; public MainWindow() { InitializeComponent(); } public List<DataItem> Data { get { if (_data == null) { _data = DataCreator.CreateData(); } return _data; } }
You have successfully bound FlexChart to the data source.