绑定DataPoint颜色

时间:2014-07-04 03:37:23

标签: c# wpf wpftoolkit

以下是我用于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>

我想在运行时设置它,而不是对Background属性进行硬编码。如何将背景绑定到LineSeries的背景?

1 个答案:

答案 0 :(得分: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