我正在尝试使用 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; }
如何让它绑定到 aList()而不是a 的索引int?
编辑
我已经能够通过命名其索引从后面的代码中获取列表中的特定值,但是在LineSeries创建图表时会动态生成多个。我想将每个绑定到 a 的索引List<int>(),我每隔一秒左右重新创建一次。
这是MemoryStats用于更新 UI 的完整方法。它通过将所有LineSeriesY 值更新为单个 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图表TimeStamp的XLineSeries轴List()为List()
我将尝试实现一个类型转换器,但我不完全确定何时/何地这样做。
编辑 2
我让它按照我想要的方式工作!我从这个 SO question 中找到了很多关于在折线图中使用多个系列的帮助。
看起来类型转换器也可以工作,所以 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将返回列表。然后,该列表中的项目将显示在图表上。