自定义标签提供程序:无法重写Init()方法
我想通过我的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
}
}
我建议通过缩放因子进入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用于试驾。
我给了类似的尝试,但有一些麻烦得到它的工作。很高兴听到它应该是一个可行的解决方案!我今天会看看它。感谢你的回答! –
我设法绕过这个问题,现在只需创建几个硬编码标签提供程序。这似乎工作正常,但我不知道这是否是正确的方法。 我还需要在每个方法中注释** Init()**方法。我必须在这里错过一些东西。 :) –