0
我花了幾天的時間嘗試將我的數據模型綁定到行系列。我工作正常;不過,我想改變線條的顏色。我知道在哪裏改變顏色,但圖表和系列會忽略我的裝訂(是一個SolidColorBrush)。如果我在XAML中對顏色進行了硬編碼,它會起作用;然而,如果我試圖將相同的屬性綁定到我的視圖模型中的顏色屬性,它將無法正常工作。花了太多時間後,我放棄了兩個原因。WPF工具包 - 代碼背後的綁定行系列
- 它只是不會工作
- 我意識到我將需要綁定「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);