[英]how to create chart control in code behind wpf
I have a chart control in xaml everythings work fine but now I want create this chart using code-behind: this is my xaml: 我在xaml中有一个图表控件,一切正常,但现在我想使用代码隐藏功能创建此图表:这是我的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 在我看来,在XAML代码中3个控件相互内部
chart:ClusteredColumnChart --> chart:ClusteredColumnChart.Series --> chart:ChartSeries chart:ClusteredColumnChart-> chart:ClusteredColumnChart.Series-> chart:ChartSeries
but in Code-behind I couldnt find this 3 controls and just used 2 controls 但是在后台代码中我找不到这3个控件,而只使用了2个控件
ClusteredColumnChart --> ChartSeries ClusteredColumnChart-> ChartSeries
You can not use "{Binding}" in Code. 您不能在代码中使用“ {Binding}”。
You have to create a Binding using 您必须使用创建绑定
new System.Windows.Data.Binding(...)
see: https://docs.microsoft.com/en-us/dotnet/api/system.windows.data.binding.-ctor?view=netframework-4.7.2 请参阅: https : //docs.microsoft.com/zh-cn/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. 更新:并回答您的第二个问题:<chart:ClusteredColumnChart.Series>是属性而不是对象。
Update 2: Binding example: 更新2:绑定示例:
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: 或者,如果您想直接设置ItemsSource,只需使用它而无需任何绑定:
series.ItemsSource = dt;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.