我解决了这个问题,在代码后面重新创建图表,而不是在XAML中使用绑定。
Chart chartUsers = new Chart();
chartUsers.Palette = palette;
chartUsers.SetValue(Grid.RowProperty, 0);
chartUsers.SetValue(Grid.ColumnProperty, 0);
chartUsers.Title = Resources["DashboardUsersTitle"].ToString();
chartUsers.Style = (Style)Application.Current.Resources["ChartStyle1"]; //my style
chartUsers.Height = 250;
gridFirstRow.Children.Add(chartUsers);
现在你可以创建系列,轴和绑定的东西给他们。
您可以将自定义颜色添加到调色板,像这样(例如):
private void CreateChartPalette()
{
List<Color> colors = new List<Color>();
colors.Add(Color.FromArgb(255, 88, 113, 165));
colors.Add(Color.FromArgb(255, 78, 149, 51));//dark green
colors.Add(Color.FromArgb(255, 32, 153, 161));//green-blue
colors.Add(Color.FromArgb(255, 201, 152, 0));//orange
colors.Add(Color.FromArgb(255, 166, 22, 22));//ref
colors.Add(Color.FromArgb(255, 196, 192, 85));//yellow
colors.Add(Color.FromArgb(255, 86, 168, 117));//light green
colors.Add(Color.FromArgb(255, 117, 49, 49));//dark red
colors.Add(Color.FromArgb(255, 195, 146, 0));//orange 2
colors.Add(Color.FromArgb(255, 128, 126, 86));//dark yellow
colors.Add(Color.FromArgb(255, 86, 121, 128));//dark blue
colors.Add(Color.FromArgb(255, 58, 115, 25));//dark green
colors.Add(Color.FromArgb(255, 126, 147, 152));//grey-blue
colors.Add(Color.FromArgb(255, 111, 169, 133));//light green 2
colors.Add(Color.FromArgb(255, 97, 55, 96));//purple
colors.Add(Color.FromArgb(255, 250, 229, 166));//yellow
colors.Add(Color.FromArgb(255, 72, 109, 117));//dark blue 2
palette = new ResourceDictionaryCollection();
Random rand = new Random();
for (int i = 0; i < colors.Count; i++)
{
LinearGradientBrush lgb = new LinearGradientBrush();
lgb.StartPoint = new Point(0, 0);
lgb.EndPoint = new Point(0.5, 1);
GradientStop gsBegin = new GradientStop();
gsBegin.Color = Color.FromArgb(128, colors[i].R, colors[i].G, colors[i].B);
gsBegin.Offset = 0;
GradientStop gsEnd = new GradientStop();
gsEnd.Color = colors[i];
gsEnd.Offset = 1;
lgb.GradientStops.Add(gsBegin);
lgb.GradientStops.Add(gsEnd);
Style style = new Style(typeof(DataPoint));
style.Setters.Add(new Setter(BackgroundProperty, lgb));
ResourceDictionary dictionary = new ResourceDictionary();
dictionary.Add("DataPointStyle", style);
palette.Add(dictionary);
}
}
您可以创建多个样式'DataPoint',并明确将它们设置为系列'DataPointStyle'广告载体。 – vorrtex