[英]WPF Toolkit Chart Axes cannot be set through Style
I am using the WPF Toolkit ( System.Windows.Controls.DataVisualization.Toolkit
) to generate a simple chart. 我正在使用WPF Toolkit(
System.Windows.Controls.DataVisualization.Toolkit
)生成一个简单的图表。 In order to set my Y-axis to start from a value of zero, I set the Chart.Axes
property like so: 为了将我的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>
This works fine. 这很好用。 However, when I try to set this property through a
Style
, intellisense does not even show Axes
. 但是,当我尝试通过
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>
If I run the code, I get an ArgumentNullException
saying Property
cannot be null. 如果我运行代码,我得到一个
ArgumentNullException
说Property
不能为null。 This is Style.Setter.Property
. 这是
Style.Setter.Property
。 I looked into the source code at Codeplex and found the Axes
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);
}
}
It says here that Setter is public but I cannot find any such public method. 它在这里说Setter是公开的,但我找不到任何这样的公共方法。 Now my questions are:
现在我的问题是:
Axes
property through a Style? Axes
属性? you're close :) 你很亲密:)
You have to attach the style to the linearAxis itself, as there is not accessor from the chart style. 您必须将样式附加到linearAxis本身,因为图表样式中没有访问者。
Style goes like this: 风格如下:
<Style x:Key="linearAxisStyle" TargetType="{x:Type charting:LinearAxis}">
<Setter Property="Orientation" Value="Y" />
<Setter Property="Minimum" Value="0" />
</Style>
Binding goes like this: 绑定是这样的:
<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}" />
I'm answering in a new item since you changed the request.... 因为你改变了请求,我正在回答一个新项目....
You expect the default syntax to be something like this: 您希望默认语法是这样的:
<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>
The problem (which actually isnt one) is that the "Axes"-element doesn't have a style-property. 问题(实际上不是一个)是“Axes”元素没有样式属性。 Therefore you can not set a style inherited by its child - the LinearAxis.
因此,您无法设置其子项继承的样式 - LinearAxis。 That's why you receive the error: "property can not be null".
这就是你收到错误的原因:“属性不能为空”。 Of course it cannot, cause its not existing.
当然它不能,因为它不存在。
So the final answer to your request is - (unfortunatly) its not possible. 所以你的要求的最终答案是 - (不幸的是)它是不可能的。 Hopefully this gives you a better understanding.
希望这能让您更好地理解。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.