1

I have a chart control in xaml everythings work fine but now I want create this chart using code-behind: this is my xaml:

<chart:ClusteredColumnChart>
    <chart:ClusteredColumnChart.Series>
        <chart:ChartSeries
            Name = "chart"
            DisplayMember = "Date"
            ItemsSource = "{Binding}"
            ValueMember = "Scores" />
    </chart:ClusteredColumnChart.Series>
</chart:ClusteredColumnChart >

I wrote this code but data not generate

ClusteredColumnChart chart = new ClusteredColumnChart();
ChartSeries series = new ChartSeries
{
    DisplayMember = "Date",
    ItemsSource = "{Binding}",
    ValueMember = "Scores"
};
series.ItemsSource = dt;
chart.Series.Add(series);
maingrid.Children.Add(chart);

What do I miss?
In my opinion, in xaml codes 3 controls are inside each other

chart:ClusteredColumnChart --> chart:ClusteredColumnChart.Series --> chart:ChartSeries

but in Code-behind I couldnt find this 3 controls and just used 2 controls

ClusteredColumnChart --> ChartSeries

Hossein Golshani
  • 1,847
  • 5
  • 16
  • 27

1 Answers1

1

You can not use "{Binding}" in Code.

You have to create a Binding using

new System.Windows.Data.Binding(...)

see: https://learn.microsoft.com/en-us/dotnet/api/system.windows.data.binding.-ctor?view=netframework-4.7.2

Update: And to answer your second question: < chart:ClusteredColumnChart.Series > is an attribute not an object.

Update 2: Binding example:

var b = new System.Windows.Data.Binding {Source = dt};
series.SetBinding(ChartSeries.ItemsSourceProperty, b);

Or if you want to set the ItemsSource directly just use this without any Bindings:

series.ItemsSource = dt;
Markus
  • 2,184
  • 2
  • 22
  • 32
  • i used this code var b = new Binding() { Source = dt }; itemsource = b; but not work get error cant convert binding to IEnumerable –  Sep 23 '18 at 10:31