3

我在嘗試有條件地格式化LineSeries(來自Silverlight 4工具包)的NumericAxis軸中出現的數字。更具體地說,我希望數字> = 10000和< = 0.0001以科學記數法顯示,但我似乎無法完成此項工作。如何在Silverlight Toolkit中有條件地格式化軸值LineSeries

我可以覆蓋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> 

如果我能指定「FormattedContent」的轉換器NumericAxisLabelStyle就像我爲LineDataPoint模板做的那樣......當然必須有一種方法!

任何想法?

在此先感謝您的幫助!

回答

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> 
+0

是的,這個伎倆。它只能說明在模板控制時理解DataContext的作用是多麼重要! – 2011-06-20 19:24:19

2

它也可以覆蓋從工具包的DisplayAxis類PrepareAxisLabel()方法。

原始方法(發現here)的源代碼是:

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

通過這種方式,你可以得到完全控制通過標籤創建。

+0

謝謝,克里斯。這可以與我正在嘗試做的其他事情派上用場。我在提高你的答案:-) – 2011-07-18 18:26:44

相關問題