如何在 Silverlight Toolkit LineSeries 中有条件地设置轴值格式
我正在尝试有条件地格式化出现在 LineSeries 的 NumericAxis 轴中的数字(来自 Silverlight 4 Toolkit)。更具体地说,我希望 >=10000 和 <=0.0001 的数字以科学计数法显示,但我似乎无法完成这项工作。
我可以像这样覆盖 NumericAxisLabel 模板:
<Style x:Key="NumericAxisLabelStyle" TargetType="chartingToolkit:NumericAxisLabel">
<Setter Property="IsTabStop" Value="False"/>
<Setter Property="StringFormat" Value="{}{0:0.0E+00}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="chartingToolkit:NumericAxisLabel">
<TextBlock Text="{TemplateBinding FormattedContent}"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
但这会将科学记数法格式应用于轴中的所有标签。我想要的是只有当我上面提到的条件发生时,字符串格式表达式才能“启动”。
通过使用与自定义值转换器的绑定,我能够在 LineDataPoint 工具提示模板中相当轻松地完成此操作,如下所示:
<ControlTemplate TargetType="chartingToolkit:LineDataPoint">
<Grid x:Name="Root" Opacity="0">
<ToolTipService.ToolTip>
<StackPanel Margin="2,2,2,2">
<StackPanel Orientation="Horizontal">
<TextBlock Text="X:" />
<ContentControl Content="{Binding objResultValueX, Converter={StaticResource ToCustomStringFormat}}"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Y:" />
<ContentControl Content="{Binding dblResultValueY, Converter={StaticResource ToCustomStringFormat}}"/>
</StackPanel>
</StackPanel>
</ToolTipService.ToolTip>
...
</Grid>
</ControlTemplate>
如果我可以像为 LineDataPoint 模板所做的那样,为 NumericAxisLabelStyle 中的“FormattedContent”指定转换器...肯定有办法的!
有什么想法吗?
预先感谢您的帮助!
I am trying to conditionally format the numbers that appear in a NumericAxis axis for a LineSeries (from Silverlight 4 Toolkit). To be more specific, I want numbers that are >=10000 and <=0.0001 to display in scientific notation, but I can't seem to make this work.
I can override the NumericAxisLabel template like this:
<Style x:Key="NumericAxisLabelStyle" TargetType="chartingToolkit:NumericAxisLabel">
<Setter Property="IsTabStop" Value="False"/>
<Setter Property="StringFormat" Value="{}{0:0.0E+00}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="chartingToolkit:NumericAxisLabel">
<TextBlock Text="{TemplateBinding FormattedContent}"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
But this will apply the scientific notation format to ALL labels in the axis. What I want is for the string format expression to "kick in" only when the condition I mentioned above occurs.
I was able to accomplish this in the LineDataPoint tooltip template fairly easy by using a binding with a custom value converter, like this:
<ControlTemplate TargetType="chartingToolkit:LineDataPoint">
<Grid x:Name="Root" Opacity="0">
<ToolTipService.ToolTip>
<StackPanel Margin="2,2,2,2">
<StackPanel Orientation="Horizontal">
<TextBlock Text="X:" />
<ContentControl Content="{Binding objResultValueX, Converter={StaticResource ToCustomStringFormat}}"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Y:" />
<ContentControl Content="{Binding dblResultValueY, Converter={StaticResource ToCustomStringFormat}}"/>
</StackPanel>
</StackPanel>
</ToolTipService.ToolTip>
...
</Grid>
</ControlTemplate>
If only I could specify a converter for the "FormattedContent" in the NumericAxisLabelStyle like I do for LineDataPoint template...surely there must be a way!
Any ideas?
Thanks in advance for the help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试将 TextBlock 的 DataContext 设置为 FormattedContent。然后将转换器应用到 Text 属性,如下所示:
Try setting the DataContext of the TextBlock to FormattedContent. Then apply the converter to the Text property as so:
还可以重写 Toolkit 的 DisplayAxis 类中的PrepareAxisLabel() 方法。
原始方法的源代码(找到此处)是:
因此,您可以使用以下内容覆盖它:
通过这种方式,您可以在创建标签时完全控制它。
It's also possible to override the PrepareAxisLabel() method from the Toolkit's DisplayAxis class.
The source code for the original method (found here) is:
So, you can override it with something like:
In this way, you can get total control over the label as it's created.