[英]WPF Toolkit Chart Axes cannot be set through Style
我正在使用WPF Toolkit( 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
设置此属性时,intellisense甚至不显示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
不能为null。 这是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是公开的,但我找不到任何这样的公共方法。 现在我的问题是:
Axes
属性? 你很亲密:)
您必须将样式附加到linearAxis本身,因为图表样式中没有访问者。
风格如下:
<Style x:Key="linearAxisStyle" TargetType="{x:Type charting:LinearAxis}">
<Setter Property="Orientation" Value="Y" />
<Setter Property="Minimum" Value="0" />
</Style>
绑定是这样的:
<chartingToolkit:Chart Width="800" Height="400" Title="Usage" Style="{StaticResource ChartStyle}">
<chartingToolkit:Chart.Axes>
<chartingToolkit:LinearAxis Style="{StaticResource linearAxisStyle}" />
<chartingToolkit:Chart.Axes/>
<chartingToolkit:LineSeries DependentValuePath="Value" IndependentValuePath="Key" ItemsSource="{Binding Data}" />
因为你改变了请求,我正在回答一个新项目....
您希望默认语法是这样的:
<Style x:Key="linearAxisStyle_Alt" TargetType="{x:Type charting:Chart}">
<Setter Property="Axes">
<Setter.Value>
<Setter Property="LinearAxis">
<Setter.Value>
<charting:LinearAxis Orientation="Y" Minimum="0" />
</Setter.Value>
</Setter>
</Setter.Value>
</Setter>
</Style>
问题(实际上不是一个)是“Axes”元素没有样式属性。 因此,您无法设置其子项继承的样式 - LinearAxis。 这就是你收到错误的原因:“属性不能为空”。 当然它不能,因为它不存在。
所以你的要求的最终答案是 - (不幸的是)它是不可能的。 希望这能让您更好地理解。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.