我解決了這個問題,在代碼後面重新創建圖表,而不是在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