【发布时间】:2013-08-01 13:03:03
【问题描述】:
我正在尝试使用 WPF 工具包创建图表,其中 Y 轴由 List() 中的值更新。我正在尝试通过特定索引访问该值。目前,绑定到List() 而不是int 的索引会创建“没有合适的轴可用于绘制相关值”。例外。
这是我目前所拥有的,请注意我尝试让 DependentValuePath 访问索引:
<Charting:LineSeries VerticalAlignment="Stretch"
HorizontalAlignment="Stretch"
ItemsSource="{Binding Path=MemoryStats}"
IndependentValuePath="Timestamp"
DependentValuePath="ByteCount[0]"
Title="Data Points">
这是 MemoryStats 值在后面的代码中包含的内容:
public List<int> ByteCount { get; set; }
public DateTime Timestamp { get; set; }
当 XAML 中的 LineSeries 具有属性 DependentValuePath="ByteCount" 并且代码隐藏使用简单的 int 时,图表可以正常工作:
public int ByteCount { get; set; }
public DateTime Timestamp { get; set; }
如何让它绑定到 List() 而不是 int 的索引?
编辑
我已经能够通过命名它的索引从后面的代码中获取列表中的具体值,但是在创建图表时会动态生成多个LineSeries。我想将每一个绑定到List<int>() 的索引,我每隔一秒左右重新创建一次。
这是MemoryStats 用来更新用户界面的完整方法。它通过将所有 LineSeries Y 值更新为单个 ByteCount int 来工作,因此目前所有行看起来都一样。
public class MemorySample
{
public static MemorySample Generate(List<int> dataPoints)
{
return new MemorySample
{
ByteCount = dataPoints[0],
Timestamp = DateTime.Now
};
}
public int ByteCount { get; set; }
public DateTime Timestamp { get; set; }
}
当然,我希望所有LineSeries 都不同。我想让图表的每个LineSeries 的 X 轴为TimeStamp(所以它们都有相同的时间戳),并且各种LineSeries 的 Y 轴值由整数的List() 更新,每个都使用List()的单独索引
我将尝试实现一个类型转换器,但我不完全确定何时/何地这样做。
编辑 2
我让它按照我想要的方式工作!找到了很多帮助from this S.O. question regarding using multiple series in a line chart.
看起来好像类型转换器也可以工作,所以 Shimrod 已经回答了这个问题。然而,我最终做的是将 LineSeries 的ItemSource 绑定到一个索引,然后检索该索引内容的数据。
所以,LineSeries 是这样的:
<Charting:LineSeries VerticalAlignment="Stretch"
HorizontalAlignment="Stretch"
ItemsSource="{Binding [0]}"
IndependentValuePath="X"
DependentValuePath="Y"
Title="Data Points">
</Charting:LineSeries>
注意ItemSource 绑定中的索引。在后面的代码中,我将控件的DataContext 设置为ObservableCollection,它包含一个从“IList”继承的对象(您可以使用任何这样做的对象),并且该对象包含包含属性 X 和 Y 属性。
public ObservableCollection<InheritsFromIList<ObjectWithXandYProperties>> VariableDataContextIsSetTo { get; set; }
访问ObservableCollection 的特定索引将返回列表。然后,该列表中的项目将显示在图表上。
【问题讨论】:
标签: c# .net wpf xaml wpftoolkit