【问题标题】:Overriding DataPointStyle in a WPF Toolkit Chart在 WPF 工具包图表中覆盖 DataPointStyle
【发布时间】:2011-02-18 14:30:10
【问题描述】:

我想在我的 WPF 工具包Chart 中覆盖LineSeriesDataPointStyle

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

但是,当我这样做时,我失去了每个系列都有不同颜色的自动调色板着色。应用DataPointStyle 会使它们全部变为橙色。

【问题讨论】:

    标签: wpf wpftoolkit


    【解决方案1】:

    在有人提出更好的方法之前,我已经手动设置了颜色。我想我暂时不会使用自动调色板。

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

    【讨论】:

      【解决方案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);
      

      【讨论】:

      • 如果你想获得每个背景系列的颜色:ResourceDictionary rd = mcLineChart.Palette[mcLineChart.Series.Count % mcLineChart.Palette.Count]; var myBrush = rd["Background"] as Brush; ... SetStyleProperty(sty, Control.BackgroundProperty, myBrush);
      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-03
        • 2013-03-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多