2014-07-04 70 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