2014-07-04 96 views
0

以下是我使用的LineSeries的DataPointStyle風格:綁定數據點顏色

<Style x:Key="LineDataPointStyle" TargetType="chrt:LineDataPoint"> 
    <Setter Property="Background" Value="#0077CC" /> 
    <Setter Property="BorderBrush" Value="White" /> 
    <Setter Property="BorderThickness" Value="2" /> 
    <Setter Property="IsTabStop" Value="False" /> 
    <Setter Property="Height" Value="10" /> 
    <Setter Property="Width" Value="10" /> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="chrt:LineDataPoint"> 
       <Grid x:Name="Root" Opacity="1"> 
        <ToolTipService.ToolTip> 
         <StackPanel Margin="2,2,2,2"> 
          <ContentControl Content="{TemplateBinding IndependentValue}" ContentStringFormat="X-Value: {0:HH:mm:ss}"/> 
          <ContentControl Content="{TemplateBinding DependentValue}" ContentStringFormat="Y-Value: {0:###.###}"/> 
         </StackPanel> 
        </ToolTipService.ToolTip> 
        <Ellipse StrokeThickness="{TemplateBinding BorderThickness}" Stroke="{TemplateBinding BorderBrush}" Fill="{TemplateBinding Background}"/> 
       </Grid> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

而不是硬編碼的背景屬性,我想在運行時進行設置。如何將背景綁定到LineSeries的背景?

+0

你想根據你的財產改變背景?你可以使用觸發器來做到這一點 – Nitin

+0

@nit:我正在定義DataPoint的默認樣式。但是如果圖表中有多個系列,則線條的顏色應該可以設置 – Abhishek

回答

0

請嘗試以下解決方法。

資源

<Window.Resources> 
    <Style x:Key="DataPointStyle" TargetType="chartingToolkit:DataPoint"> 
     <Setter Property="Background" Value="{DynamicResource ChartLineColor}"/> 
     <Setter Property="BorderThickness" Value="2"/> 
     <Setter Property="BorderBrush" Value="White"/> 
     <Setter Property="IsTabStop" Value="False"/> 
     <Setter Property="Height" Value="10" /> 
     <Setter Property="Width" Value="10" /> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="chartingToolkit:LineDataPoint"> 
        <Grid x:Name="Root" Opacity="1"> 
         <ToolTipService.ToolTip> 
          <StackPanel Margin="2,2,2,2"> 
           <ContentControl Content="{TemplateBinding IndependentValue}" ContentStringFormat="Date : {0}"/> 
           <ContentControl Content="{TemplateBinding DependentValue}" ContentStringFormat="Count : {0:###,###,###}"/> 
          </StackPanel> 
         </ToolTipService.ToolTip> 
         <Ellipse StrokeThickness="{TemplateBinding BorderThickness}" Stroke="{TemplateBinding BorderBrush}" Fill="{TemplateBinding Background}"/> 
        </Grid> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
</Window.Resources> 

XAML

<Grid> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition/> 
     <ColumnDefinition/> 
    </Grid.ColumnDefinitions> 
    <chartingToolkit:Chart x:Name="LineChart" Title="Demo Chart"> 
     <chartingToolkit:LineSeries DependentValuePath="Value" DataPointStyle="{StaticResource DataPointStyle}" IndependentValuePath="Key" ItemsSource="{Binding}" IsSelectionEnabled="True"> 
      <chartingToolkit:LineSeries.Resources> 
       <SolidColorBrush x:Key="ChartLineColor" Color="Green"/> 
      </chartingToolkit:LineSeries.Resources> 
     </chartingToolkit:LineSeries> 
    </chartingToolkit:Chart> 

    <chartingToolkit:Chart x:Name="LineChart1" Grid.Column="1" Title="Demo Chart"> 
     <chartingToolkit:LineSeries DependentValuePath="Value" DataPointStyle="{StaticResource DataPointStyle}" IndependentValuePath="Key" ItemsSource="{Binding}" IsSelectionEnabled="True"> 
      <chartingToolkit:LineSeries.Resources> 
       <SolidColorBrush x:Key="ChartLineColor" Color="Red"/> 
      </chartingToolkit:LineSeries.Resources> 
     </chartingToolkit:LineSeries> 
    </chartingToolkit:Chart> 
</Grid> 

結果

enter image description here