3

我正在尝试有条件地格式化出现在 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”指定一个转换器就好了……肯定有办法!

有任何想法吗?

在此先感谢您的帮助!

4

2 回答 2

4

尝试将 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> 
于 2011-06-20T19:19:18.293 回答
2

也可以覆盖 Toolkit 的 DisplayAxis 类中的 PrepareAxisLabel() 方法。

原始方法的源代码(在此处找到)是:

protected virtual void PrepareAxisLabel(Control label, object dataContext)
    {
        label.DataContext = dataContext;
        label.SetStyle(AxisLabelStyle);
    }

因此,您可以使用以下内容覆盖它:

public class MyLinearAxis : LinearAxis
{     
    protected override void PrepareAxisLabel(Control label, object dataContext)
    {   
        (label as AxisLabel).StringFormat = "{0:c}";   // currency format, for example
        dataContext = 10.0;                            // your own custom numeric value

        base.PrepareAxisLabel(label, dataContext);
    }
}

通过这种方式,您可以在创建标签时完全控制它。

于 2011-06-20T19:46:02.633 回答