【问题标题】:Can I draw dashed lines in WPF Toolkit chart?我可以在 WPF Toolkit 图表中绘制虚线吗?
【发布时间】:2012-08-09 12:34:30
【问题描述】:

在我的基于 WPF 的应用程序中,我正在使用 WPF Toolkit 中提供的数据可视化图表组件。我想画虚线,类似于this的插图所以回答:

不幸的是,这仅适用于 Windows 窗体,因为 BorderDashStyle 属性仅存在于 DataVisualization.Charting 组件的 Windows 窗体版本中,而不存在于等效的 WPF 工具包中。

我应该如何使用 WPF Toolkit 图表组件生成虚线?

【问题讨论】:

    标签: c# wpf wpftoolkit mschart


    【解决方案1】:

    我搜索了Silverlight Toolkit 图表组件的类似解决方案,并找到了this

    幸运的是,事实证明同样的方法可以应用在 WPF 中。通过将属性LineSeries.PolylineStyle 设置为System.Windows.Shapes.Polyline 样式并使用合适的Shape.StrokeDashArray 属性设置,可以获得所需的虚线。

    以编程方式,可以通过以下方式完成:

    var series = new LineSeries
        {
             ItemsSource = calcData,
             IndependentValuePath = "X",
             DependentValuePath = "Y",
             PolylineStyle = GetDashedLineStyle()
        };
    
    ...
    
    Style GetDashedLineStyle()
    {
        var style = new Style(typeof(Polyline));
        style.Setters.Add(new Setter(Shape.StrokeDashArrayProperty, 
                          new DoubleCollection(new[] { 5.0 })));
        return style;
    }
    

    【讨论】:

    • 有用 - 感谢您回答您自己的问题!我也用过这个link
    • 经过 3 小时的搜索找到了这个。谢谢.. 从上面引用的链接复制
    【解决方案2】:

    在 WPF 中添加到 xaml 的另一种方法:

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:chartingToolkit="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"
    
    ...
    
    
        <Window.Resources>
            <Style x:Key="DashedPolyLine" TargetType="{x:Type Polyline}">                        
                <Setter Property="StrokeDashArray" Value="2 3 2" />
            </Style>        
        </Window.Resources>
    
    ...
    
    <chartingToolkit:LineSeries  Title="Title" DependentValuePath="Value" IndependentValuePath="Key" ItemsSource="{Binding BindingValue}" PolylineStyle="{StaticResource DashedPolyLine}"/>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-08
      相关资源
      最近更新 更多