【问题标题】:Having trouble binding data from C# to WPF to draw a line graph无法将数据从 C# 绑定到 WPF 以绘制折线图
【发布时间】:2020-08-11 20:54:43
【问题描述】:

背后的代码 - 我有一个可观察的点集合,其中包含要绘制到图表的数据。此数据是从外部源读取的,每个列表中的值都在 20k-80k 之间。

ObservableCollection<System.Windows.Point> listChannelData =new ObservableCollection <System.Windows.Point>();
ObservableCollection<System.Windows.Point> listActiveBaseline =new ObservableCollection <System.Windows.Point>();

一旦填充了这些列表,我就将系列 ItemSource 设置为这些列表,如下所示:

seriesChannelData.ItemsSource = listChannelData;
seriesActiveBaseline.ItemsSource = listActiveBaseline;

XAML - 刚开始学习时不确定XAML是否正确,我也不知道是在代码中还是在XAML中绑定到Observable Collection更好。

<DVC:LineSeries Name="seriesChannelData" DependentValuePath="Y" IndependentValuePath="X" ItemsSource="{Binding seriesChannelData}" IsSelectionEnabled="True"/>
<DVC:LineSeries Name="seriesActiveBaseline" DependentValuePath="Y" IndependentValuePath="X" ItemsSource="{Binding}" IsSelectionEnabled="True" />

当我尝试运行这个程序时,似乎需要很长时间才能填充列表而没有任何图表,所以我的数据绑定似乎是错误的。我想知道的另一件事是,如果我在填充列表后将系列项目源设置为错误。

【问题讨论】:

  • 我研究了很多次,但代码总是最好的解决方案。不过真的很痛苦。
  • 不确定您的确切问题是什么,但绑定到字段将不起作用。它们必须是属性。
  • @Clemens 嗨 Clemens,我已更新问题以提供更多详细信息,我们将不胜感激。

标签: c# wpf charts data-binding data-visualization


【解决方案1】:
<DVC:Chart Name="seriesChannelData" VerticalAlignment="Top" Height="200"
DataContext="{Binding seriesChannelData}" IsTabStop="True" Background="White">
    <DVC:LineSeries DependentValuePath="Y" IndependentValuePath="X"
ItemsSource="{Binding}">
    </DVC:LineSeries>
</DVC:Chart>

DataContext 中,您将绑定可观察集合名称,在DependentValuePathIndependentValuePath 中,您将绑定可观察集合的成员。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-20
    • 2014-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多