0

我花了几天时间尝试将我的数据模型绑定到一个系列。我工作正常;但是,我想更改线条颜色。我知道在哪里更改颜色,但图表和系列会忽略我的绑定(是 SolidColorBrush)。如果我在 XAML 中对颜色进行硬编码,它会起作用;但是,如果我尝试将相同的属性绑定到我的视图模型中的颜色属性,它将不起作用。花了太多时间后,我放弃了两个原因。

  1. 它只是行不通
  2. 我意识到我需要将“x”个视图模型绑定到图表上,以便一次显示多个线系列。

我最终只是在后面的代码中定义了我的系列,就像这样......

LineSeries BuildLine(DosePointsViewModel model)
    {
        LineSeries series = new LineSeries();

        // styles
        Style poly = new Style(typeof(Polyline));
        poly.Setters.Add(new Setter(Polyline.StrokeProperty, model.LineColor));
        poly.Setters.Add(new Setter(Polyline.StrokeThicknessProperty, 3d));
        series.PolylineStyle = poly;

        Style pointStyle = new Style(typeof(LineDataPoint));
        pointStyle.Setters.Add(new Setter(LineDataPoint.BackgroundProperty, model.LineColor));
        series.DataPointStyle = pointStyle;

        // binding
        series.IsSelectionEnabled = false;
        series.IndependentValueBinding = new System.Windows.Data.Binding("Distance");
        series.DependentValueBinding = new System.Windows.Data.Binding("Dose");

        // X axis
        LinearAxis xAxis = new LinearAxis();
        xAxis.Title = "Distance";
        xAxis.ShowGridLines = false;
        xAxis.Interval = 1;
        xAxis.Orientation = AxisOrientation.X;
        series.IndependentAxis = xAxis;

        // Y axis
        LinearAxis yAxis = new LinearAxis(); //series.DependentRangeAxis as LinearAxis;
        yAxis.Maximum = 5000d;
        yAxis.Minimum = -100d;
        yAxis.Minimum = model.Points.Min(d => d.Dose) - model.Points.Min(d => d.Dose) * 0.50;
        yAxis.Maximum = model.Points.Max(d => d.Dose) + model.Points.Max(d => d.Dose) * 0.05;
        yAxis.ShowGridLines = true;
        yAxis.Orientation = AxisOrientation.Y;
        yAxis.Title = "Dose";

        Style s = new Style(typeof(Line));
        s.Setters.Add(new Setter(Line.StrokeProperty, new SolidColorBrush(Colors.LightBlue)));
        s.Setters.Add(new Setter(Line.StrokeThicknessProperty, 1d));
        yAxis.GridLineStyle = s;
        series.DependentRangeAxis = yAxis;

        return series;
    }

现在,我的线条系列的颜色有效。当然,主要原因是我直接通过...设置颜色

poly.Setters.Add(new Setter(Polyline.StrokeProperty, model.LineColor));
pointStyle.Setters.Add(new Setter(LineDataPoint.BackgroundProperty, model.LineColor));

所以,我的问题是这个。我希望能够在图表中添加多个线系列;但是,当我尝试这样做时,只有最后一项被绑定。在代码内部,这是针对正在创建的每个线系列完成的。只有最后一个线系列被添加到图表中。

DosePointsViewModel model = new DosePointsViewModel(_snc, m.Id);
            LineSeries series = BuildLine(model);
            DoseChart.Series.Clear();
            DoseChart.Series.Add(series);
4

1 回答 1

0

哇,当我阅读我的问题时,我意识到我在打电话

DoseChart.Series.Clear();

嗯,这是一个有趣的发现。

于 2017-10-11T15:30:36.520 回答