2017-04-06 71 views
1

我想通過我的CustomNumericLabelProvider訪問viewModel中的縮放因子。自定義標籤提供程序:無法重寫Init()方法

我不太清楚,最好的方法是什麼,但我想如果我用初始化(IAxis不parentAxis)方法,這是在LabelProvider所示,我也許可以通過父軸進行訪問documentation。我試過了,但現在我得到一個錯誤,告訴我「沒有合適的替代方法」

如果我註釋掉Init()方法,CustomNumericLabelProvider效果很好(具有硬編碼的縮放因子)。

任何想法爲什麼我收到此錯誤消息?或者,在我的viewModel中訪問縮放因子的另一個好方法是什麼?

注意:我也嘗試將viewModel傳遞給標籤提供程序的自定義構造函數(我能夠使用viewportManager做這樣的事情),但是這似乎不起作用。

下面的代碼(通過自定義構造函數,雖然我得到同樣的錯誤信息沒有它)

public class CustomNumericLabelProvider : SciChart.Charting.Visuals.Axes.LabelProviders.NumericLabelProvider 
{ 
    // Optional: called when the label provider is attached to the axis 
    public override void Init(IAxis parentAxis) { 
     // here you can keep a reference to the axis. We assume there is a 1:1 relation 
     // between Axis and LabelProviders 
     base.Init(parentAxis); 
    } 

    /// <summary> 
    /// Formats a label for the axis from the specified data-value passed in 
    /// </summary> 
    /// <param name="dataValue">The data-value to format</param> 
    /// <returns> 
    /// The formatted label string 
    /// </returns> 
    public override string FormatLabel(IComparable dataValue) 
    { 
     // Note: Implement as you wish, converting Data-Value to string 
     var converted = (double)dataValue * .001 //TODO: Use scaling factor from viewModel 
     return converted.ToString(); 

     // NOTES: 
     // dataValue is always a double. 
     // For a NumericAxis this is the double-representation of the data 
    } 
} 
+0

我設法繞過這個問題,現在只需創建幾個硬編碼標籤提供程序。這似乎工作正常,但我不知道這是否是正確的方法。 我還需要在每個方法中註釋** Init()**方法。我必須在這裏錯過一些東西。 :) –

回答

1

我建議通過縮放因子進入CustomNumericLabelProvider的構造函數,實例化它在你的視圖模型。

所以,你的代碼變得

public class CustomNumericLabelProvider : LabelProviderBase 
    { 
     private readonly double _scaleFactor; 

     public CustomNumericLabelProvider(double scaleFactor) 
     { 
      _scaleFactor = scaleFactor; 
     } 

     public override string FormatLabel(IComparable dataValue) 
     { 
      // TODO 
     } 

     public override string FormatCursorLabel(IComparable dataValue) 
     { 
      // TODO 
     } 
    } 

    public class MyViewModel : ViewModelBase 
    { 
     private CustomNumericLabelProvider _labelProvider = new CustomNumericLabelProvider(0.01); 

     public CustomNumericLabelProvider LabelProvider { get { return _labelProvider; } } 
    } 

然後您給它綁定如下

<s:NumericAxis LabelProvider="{Binding LabelProvider}"/> 

假設NumericAxis DataContext的是你的視圖模型。

在SciChart v5中請注意,AxisBindings中會有新的API(類似於SeriesBinding),用於在ViewModel中動態創建軸。這將使MVVM中的動態軸更容易。您可以訪問我們的WPF Chart Examples here,將SciChart v5用於試駕。

+1

我給了類似的嘗試,但有一些麻煩得到它的工作。很高興聽到它應該是一個可行的解決方案!我今天會看看它。感謝你的回答! –