【发布时间】:2013-07-16 06:50:16
【问题描述】:
我正在使用 WPF 工具包 (System.Windows.Controls.DataVisualization.Toolkit) 来生成一个简单的图表。为了将 Y 轴设置为从零值开始,我将 Chart.Axes 属性设置如下:
<chartingToolkit:Chart Width="800" Height="400" Title="Usage" Style="{StaticResource ChartStyle}">
<chartingToolkit:Chart.Axes>
<chartingToolkit:LinearAxis Orientation="Y" Minimum="0" />
</chartingToolkit:Chart.Axes>
<chartingToolkit:LineSeries DependentValuePath="Value" IndependentValuePath="Key" ItemsSource="{Binding Data}" />
</chartingToolkit:Chart>
这很好用。但是,当我尝试通过Style 设置此属性时,智能感知甚至不显示Axes。
<Style x:Key="ChartStyle" TargetType="{x:Type chartingToolkit:Chart}">
<Setter Property="Axes">
<Setter.Value>
<chartingToolkit:LinearAxis Orientation="Y" Minimum="0" />
</Setter.Value>
</Setter>
</Style>
如果我运行代码,我会得到一个 ArgumentNullException 说 Property 不能为空。这是Style.Setter.Property。我在 Codeplex 查看了源代码,发现了 Axes 属性:
[SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly", Justification = "Setter is public to work around a limitation with the XAML editing tools.")]
[SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "value", Justification = "Setter is public to work around a limitation with the XAML editing tools.")]
public Collection<IAxis> Axes
{
get
{
return _axes;
}
set
{
throw new NotSupportedException(Properties.Resources.Chart_Axes_SetterNotSupported);
}
}
这里说 Setter 是公开的,但我找不到任何这样的公开方法。现在我的问题是:
- 通过 Style 设置属性在技术上与本问题中的第一段代码有何不同?
- 有没有办法通过样式设置
Axes属性? - 我还应该使用 WPF 工具包制作图表吗?有没有更新的“经典”方法来生成我不知道的图表?
【问题讨论】:
-
您必须将样式附加到 linearAxis 本身,因为图表样式中没有访问器。
标签: c# wpf xaml dependency-properties wpftoolkit