简体   繁体   中英

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. In order to set my Y-axis to start from a value of zero, I set the Chart.Axes property like so:

<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 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. This is Style.Setter.Property . I looked into the source code at Codeplex and found the Axes property:

[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. Now my questions are:

  1. How is setting the property through a Style technically different from the first block of code in this question?
  2. Is there a way I can set the Axes property through a Style?
  3. Should I still be using the WPF Toolkit for charts? Is there a newer "canon" method to generate charts that I am not aware of?

you're close :)

You have to attach the style to the linearAxis itself, as there is not accessor from the chart style.

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. Therefore you can not set a style inherited by its child - the 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM