1

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

我不太确定最好的方法是什么,但我想如果我使用LabelProvider文档中显示的Init(IAxis parentAxis)方法,我可能能够通过父轴访问它。我已经尝试过了,但现在我收到一个错误消息,告诉我"There is no proper 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
    }
}
4

1 回答 1

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的数据上下文是您的视图模型。

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

于 2017-04-07T10:07:34.963 回答