C# 数据可视化。图表无法与ObservableList一起使用
我正在尝试将C# 数据可视化。图表无法与ObservableList一起使用,c#,wpf,charts,observable,C#,Wpf,Charts,Observable,我正在尝试将System.Windows.Controls.DataVisualization.Charting图表与一些observedcollection相结合 我为一个数据点创建了类: public class ChartDataPoint : INotifyPropertyChanged { private double xvalue; public double XValue { get { return xvalue; } s
System.Windows.Controls.DataVisualization.Charting
图表与一些observedcollection
相结合
我为一个数据点创建了类:
public class ChartDataPoint : INotifyPropertyChanged
{
private double xvalue;
public double XValue
{
get { return xvalue; }
set {
if (xvalue != value)
{
xvalue = value;
NotifyPropertyChanged("XValue");
}
}
}
private double yvalue;
public double YValue
{
get { return yvalue; }
set {
if (yvalue != value)
{
yvalue = value;
NotifyPropertyChanged("YValue");
}
}
}
/// <summary>
/// Constructor
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
private ChartDataPoint(double x, double y)
{
this.XValue = x;
this.YValue = y;
}
public static ChartDataPoint CreateNew(double x, double y)
{
return new ChartDataPoint(x, y);
}
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
}
在我的窗口显示后,我正在单击一个按钮,将项目添加到我的列表中:
private void Button_Click(object sender, RoutedEventArgs e)
{
ListSeries1Data.Add(ChartDataPoint.CreateNew(i, r.Next(100)));
i++;
}
这是我的XAML图表的一部分
<c:Chart Name="Chart1" Background="WhiteSmoke">
<c:LineSeries Name="Series1"
Title="Read value"
IndependentValueBinding="{Binding Path=XValue}"
DependentValueBinding="{Binding Path=YValue}">
</c:LineSeries>
</c:Chart>
我的代码有什么问题?您需要为您的系列添加
ItemsSource
。像这样:
<chartingToolkit:Chart Name="Chart1" Background="WhiteSmoke">
<chartingToolkit:LineSeries Name="Series1"
Title="Read value"
ItemsSource="{Binding}"
IndependentValueBinding="{Binding Path=XValue}"
DependentValueBinding="{Binding Path=YValue}">
</chartingToolkit:LineSeries>
</chartingToolkit:Chart>
<chartingToolkit:Chart Name="Chart1" Background="WhiteSmoke">
<chartingToolkit:LineSeries Name="Series1"
Title="Read value"
ItemsSource="{Binding}"
IndependentValueBinding="{Binding Path=XValue}"
DependentValueBinding="{Binding Path=YValue}">
</chartingToolkit:LineSeries>
</chartingToolkit:Chart>