如何在 Silverlight Toolkit LineSeries 中有条件地设置轴值格式

发布于 2024-11-16 00:59:56 字数 2090 浏览 6 评论 0原文

我正在尝试有条件地格式化出现在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

匿名。 2024-11-23 00:59:56

尝试将 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> 

Try setting the DataContext of the TextBlock to FormattedContent. Then apply the converter to the Text property as so:

<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> 
梦幻之岛 2024-11-23 00:59:56

还可以重写 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);
    }
}

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

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:

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

So, you can override it with something like:

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);
    }
}

In this way, you can get total control over the label as it's created.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文