我正在尝试通过CustomNumericLabelProvider访问viewModel中的缩放系数。
我不太确定最好的方法是什么,但我想我可以通过父轴访问它,如果我使用 Init(IAxis parentAxis) 在LabelProvider documentation中显示的strong>方法。我试过了,但现在我收到一个错误,告诉我“没有合适的覆盖方法”。
如果我注释掉 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 :(得分:1)
我建议将缩放因子传递给CustomNumericLabelProvider的构造函数,并在viewmodel中实例化它。
所以你的代码变成了
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是您的viewmodel。
请注意,在SciChart v5中,将有新的AxisBindings API(类似于SeriesBinding),用于在ViewModel中动态创建轴。这将使MVVM中的动态轴更容易。您可以访问我们的WPF Chart Examples here,将SciChart v5用于试驾。