WPF工具包 - 使用绑定设置线系列折线颜色

时间:2015-08-15 11:12:02

标签: c# wpf binding wpftoolkit

我在我的图表中添加了LineSeries。这是代码:

<chartingToolkit:LineSeries Name="line" IndependentValueBinding="{Binding Path=Key}" DependentValueBinding="{Binding Path=Value}">
    <chartingToolkit:LineSeries.Template>
        <ControlTemplate TargetType="chartingToolkit:LineSeries">
            <Canvas x:Name="PlotArea">
                <Polyline x:Name="polyline"
              Points="{TemplateBinding Points}" 
              Stroke="Green" 
              Style="{TemplateBinding PolylineStyle}" />
            </Canvas>
        </ControlTemplate>
    </chartingToolkit:LineSeries.Template>
</chartingToolkit:LineSeries>

这没关系。我的折线是绿色的。但是当我尝试使用绑定设置Polyline.Stroke颜色时,我的线变得不可见。这是代码:

<chartingToolkit:LineSeries Name="line" IndependentValueBinding="{Binding Path=Key}" DependentValueBinding="{Binding Path=Value}">
    <chartingToolkit:LineSeries.Template>
        <ControlTemplate TargetType="chartingToolkit:LineSeries">
            <Canvas x:Name="PlotArea">
                <Polyline x:Name="polyline"
              Points="{TemplateBinding Points}" 
              Stroke="{Binding Path=MyColor}" 
              Style="{TemplateBinding PolylineStyle}" />
            </Canvas>
        </ControlTemplate>
    </chartingToolkit:LineSeries.Template>
</chartingToolkit:LineSeries>

我无法弄清楚它似乎是什么问题。

1 个答案:

答案 0 :(得分:1)

您可能使用了错误的颜色类型,您应该使用SolidColorBrush

 private SolidColorBrush _chartColor = new SolidColorBrush(Colors.Aqua);
    public SolidColorBrush ChartColor
    {
        get
        {
            return _chartColor;
        }

        set
        {
            if (_chartColor == value)
            {
                return;
            }

            _chartColor = value;
        }
    }

然后绑定应该完美地运作

<chartingToolkit:Chart   Title="Line Series"  
VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Margin="0"  >
        <chartingToolkit:LineSeries Name="line" ItemsSource="{Binding LineSeriesData}"
            IndependentValueBinding="{Binding Path=Key}"
            DependentValueBinding="{Binding Path=Value}">
            <chartingToolkit:LineSeries.Template>
                <ControlTemplate TargetType="chartingToolkit:LineSeries">
                    <Canvas x:Name="PlotArea">
                        <Polyline x:Name="polyline"
                                  Points="{TemplateBinding Points}" 
                                  Stroke="{Binding ChartColor}" 
                                  Style="{TemplateBinding PolylineStyle}" />
                    </Canvas>
                </ControlTemplate>
            </chartingToolkit:LineSeries.Template>

        </chartingToolkit:LineSeries>
    </chartingToolkit:Chart>