WPF 工具包 - 设置具有绑定的 Line Series 多段线颜色

WPF Toolkit - set Line Series polyline color with binding

我在图表中添加了 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>

我想不通这似乎是什么问题。

您可能使用了错误的颜色类型,您应该使用 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>