簡體   English   中英

無法通過Style設置WPF Toolkit Chart Axes

[英]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>

如果我運行代碼,我得到一個ArgumentNullExceptionProperty不能為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是公開的,但我找不到任何這樣的公共方法。 現在我的問題是:

  1. 如何通過技術設置屬性在技術上與此問題中的第一個代碼塊不同?
  2. 有沒有辦法可以通過Style設置Axes屬性?
  3. 我還應該使用WPF工具包來制作圖表嗎? 是否有更新的“佳能”方法來生成我不知道的圖表?

你很親密:)

您必須將樣式附加到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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM