WPF 工具包(2010 年 2 月版)当 ItemsSource 只有一项时,列系列图表为空
我遇到了一个烦人的问题:我有一个简单的图表,其中有一个 ColumnSeries 和两个轴(一个用于从属值的 LinearAxis 和一个用于独立值的 DateTimeAxis)。 我的 ColumnSeries 的 ItemsSource 绑定到 DataPoint 实例的集合(只有 3 个属性的简单类:Date、IndependentValue 和 DependentValue)。 如果集合中有 2 个或更多项目,则图表会正确显示列; 但如果只有一个,则不会显示任何列。
对发生的事情有什么想法吗?
下面是 XAML(为简洁起见,省略了标准命名空间和 WPF Toolkit 的命名空间):
<Window x:Class="Demo.MainWindow" (...) xmlns:local="clr-namespace:Demo">
<Grid>
<Grid.Resources>
<local:DataPointCollection x:Key="DataPointCollection" />
</Grid.Resources>
<ct:Chart Title="Demo">
<ct:ColumnSeries Title="A"
ItemsSource="{StaticResource DataPointCollection}"
IndependentValueBinding="{Binding Date}"
DependentValueBinding="{Binding DependentValue}" />
<ct:Chart.Axes>
<ct:LinearAxis Orientation="Y"
ShowGridLines="True"
Title="Dependent Title" />
<ct:DateTimeAxis Orientation=X"
ShowGridLines="True"
Interval="1"
IntervalType="Days" />
</ct:Chart.Axes>
</ct:Chart>
</Grid>
</Window>
DataPointCollection 类:
using System;
namespace Demo
{
using System.Collections.ObjectModel;
public class DataPointCollection: Collection<DataPoint>
{
public DataPointCollection()
{
Add(new DataPoint { Date = DateTime.Now.Date, DependentValue = 5 });
// Comment next line to see an empty chart:
Add(new DataPoint { Date = DateTime.Now.Date.AddDays(1), DependentValue = 6 });
}
}
}
和 DataPoint 类:
using System;
namespace Demo
{
public class DataPoint
{
public DateTime Date { get; set; }
public double DependentValue { get; set; }
}
}
该项目是常规 WPF 应用程序 (WPF 4)。
提前致谢。
I'm having an annoying problem: I have a simple Chart with one ColumnSeries and two axis (a LinearAxis for the dependent value and a DateTimeAxis for the independent one).
My ColumnSeries' ItemsSource is bound to a Collection of instances of DataPoint (simple class with only 3 properties: Date, IndependentValue and DependentValue).
The Chart displays the columns correctly if the Collection has 2 or more items in it; but if it only has one, it won't show any column.
Any ideas on what's going on?
Here's the XAML (with both standard and WPF Toolkit's namespaces omitted for brevity):
<Window x:Class="Demo.MainWindow" (...) xmlns:local="clr-namespace:Demo">
<Grid>
<Grid.Resources>
<local:DataPointCollection x:Key="DataPointCollection" />
</Grid.Resources>
<ct:Chart Title="Demo">
<ct:ColumnSeries Title="A"
ItemsSource="{StaticResource DataPointCollection}"
IndependentValueBinding="{Binding Date}"
DependentValueBinding="{Binding DependentValue}" />
<ct:Chart.Axes>
<ct:LinearAxis Orientation="Y"
ShowGridLines="True"
Title="Dependent Title" />
<ct:DateTimeAxis Orientation=X"
ShowGridLines="True"
Interval="1"
IntervalType="Days" />
</ct:Chart.Axes>
</ct:Chart>
</Grid>
</Window>
The DataPointCollection class:
using System;
namespace Demo
{
using System.Collections.ObjectModel;
public class DataPointCollection: Collection<DataPoint>
{
public DataPointCollection()
{
Add(new DataPoint { Date = DateTime.Now.Date, DependentValue = 5 });
// Comment next line to see an empty chart:
Add(new DataPoint { Date = DateTime.Now.Date.AddDays(1), DependentValue = 6 });
}
}
}
And the DataPoint class:
using System;
namespace Demo
{
public class DataPoint
{
public DateTime Date { get; set; }
public double DependentValue { get; set; }
}
}
The project is a regular WPF Application (WPF 4).
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
浏览 ColumnSeries 的源代码可以看出,列宽与数据范围相关,在您的情况下,列宽为零。除非您想修复工具包中的错误,否则我建议的唯一解决方法是用虚拟点包围您的单个点,使其看起来只显示一列:
Glancing at the source code for
ColumnSeries
shows that the column width is related to the range of the data, which in your case is zero. Unless you want to fix bugs in the toolkit, the only workaround I can suggest is to surround your single point with dummy points to make it look like only one column is being shown: