我正在尝试有条件地格式化出现在 LineSeries(来自 Silverlight 4 工具包)的 NumericAxis 轴中的数字。更具体地说,我希望 >=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>
但这会将科学记数法应用于轴中的所有标签。我想要的是仅当我上面提到的条件发生时字符串格式表达式才“启动”。
通过使用自定义值转换器的绑定(bind),我能够在 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”指定一个转换器就好了……肯定有办法!
有什么想法吗?
在此先感谢您的帮助!
最佳答案
尝试将 TextBlock 的 DataContext 设置为 FormattedContent。然后将转换器应用到 Text 属性:
<Style x:Key="NumericAxisLabelStyle" TargetType="chartingToolkit:NumericAxisLabel">
<Setter Property="IsTabStop" Value="False"/>
<Setter Property="Template">
<Setter.Value >
<ControlTemplate TargetType="chartingToolkit:NumericAxisLabel">
<TextBlock DataContext="{TemplateBinding FormattedContent}" Text ="{Binding Converter={StaticResource ToCustomStringFormat}}"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
关于silverlight - 如何在 Silverlight Toolkit LineSeries 中有条件地格式化轴值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6383047/