覆盖WPF工具包图表中的DataPointStyle

时间:2011-02-18 14:30:11

标签: wpf wpftoolkit

我想覆盖WPF工具包DataPointStyleLineSeries的{​​{1}}:

Chart

然而,当我这样做时,我失去了自动调色板着色,其中每个系列都有不同的颜色。应用<chart:LineSeries> <chart:DataPointSeries.DataPointStyle> <Style BasedOn="{StaticResource {x:Type chart:LineDataPoint}}" TargetType="{x:Type chart:LineDataPoint}"> <Setter Property="Width" Value="20" /> <Setter Property="Height" Value="20" /> </Style> </chart:DataPointSeries.DataPointStyle> </chart:LineSeries> 会使它们全部变为橙色。

4 个答案:

答案 0 :(得分:4)

在有人建议更好的方法之前,我手动设置颜色。我想我现在不会使用自动调色板。

<Style
    x:Key="SimpleDataPointStyle"
    BasedOn="{StaticResource {x:Type charting:LineDataPoint}}"
    TargetType="{x:Type charting:LineDataPoint}">
    <Setter Property="Width" Value="20" />
    <Setter Property="Height" Value="20" />
</Style>

...

<chart:LineSeries ... >
    <chart:DataPointSeries.DataPointStyle>
        <Style
            BasedOn="{StaticResource SimpleDataPointStyle}"
            TargetType="{x:Type charting:LineDataPoint}">
            <Setter Property="Background" Value="Green" />
        </Style>
    </chart:DataPointSeries.DataPointStyle>
</chart:LineSeries>
<chart:LineSeries ... >
    <chart:DataPointSeries.DataPointStyle>
        <Style
            BasedOn="{StaticResource SimpleDataPointStyle}"
            TargetType="{x:Type charting:LineDataPoint}">
            <Setter Property="Background" Value="Red" />
        </Style>
    </chart:DataPointSeries.DataPointStyle>
</chart:LineSeries>

答案 1 :(得分:2)

对于那些感兴趣的人,也可以在后面的代码中完成添加新的LineSeries,如下所示:

ResourceDictionary rd = MyChart.Palette[MyChart.Series.Count % MyChart.Palette.Count];
Style style = new Style(typeof(LineDataPoint), rd["DataPointStyle"] as Style);
style.Setters.Add(new Setter(OpacityProperty, 0.0));

LineSeries ls = new LineSeries()
{
    DataPointStyle = style
};
MyChart.Series.Add(ls);

答案 2 :(得分:1)

我刚刚发布了此here的解决方案。

答案 3 :(得分:-2)

而不是<Setter Property="Background" Value="Green" /> 只需将值绑定到颜色作为模型的属性。所以<Setter Property="Background" Value="{Binding Path=Color}" />