自定义标签提供程序:无法覆盖 Init() 方法

Custom Label Provider: Can't override Init() method

我正在尝试通过我的 CustomNumericLabelProvider 访问 viewModel 中的比例因子。

我不太确定最好的方法是什么,但我想如果我使用 Init(IAxis parentAxis) 我可以通过父轴访问它 方法显示在 LabelProvider documentation 中。我试过了,但现在我收到一条错误消息,告诉我 "There is no suitable method for override".

如果我注释掉 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
    }
}

我建议将比例因子传递给 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 的数据上下文是您的视图模型。

请注意,在 SciChart v5 中将有新的 AxisBindings API(类似于 SeriesBinding),用于在 ViewModel 中动态创建轴。这将使 MVVM 中的动态轴更容易。您可以通过访问我们的 WPF Chart Examples here 来试用 SciChart v5。