Exception when adding palettes in code behind

3 posts, 0 answers
  1. Håkan
    Håkan avatar
    183 posts
    Member since:
    May 2011

    Posted 17 Nov 2014 Link to this post


    I have a pie chart that I need to control the colors on.
    Therefore I add palettes in code behind.
    It works fine the first time, but when I update the data and the chart is redrawn it crashes when adding the second entry of a palette.
    Although the palette gets added correctly, so if I surround it with a try/catch it works, but I would like to solve the problem in the first case.

    Here is my code:

                List<ShiftTypesQuotaGaugeChartData> data = new List<ShiftTypesQuotaGaugeChartData>();
                List<int> shiftTypeIds = planningViewModel.Appointments.Where(a => a.IsVisible).Select(a => a.ShiftTypeId).Distinct().ToList();
                if (shiftTypeIds.Count == 0)
                foreach (int shiftTypeId in shiftTypeIds)
                    int shifts = planningViewModel.Appointments.Where(a => a.ShiftTypeId == shiftTypeId).Count();
                    if (shifts > 0)
                        ShiftTypeDTO st = this.allShiftTypes.Where(s => s.ShiftTypeId == shiftTypeId).FirstOrDefault();
                        data.Add(new ShiftTypesQuotaGaugeChartData()
                            Name = st.Name,
                            Value = shifts,
                            this.Chart.Palette.GlobalEntries.Add(new PaletteEntry(GraphicsUtility.GetBrushFromString(st.Color), GraphicsUtility.GetBrush(GraphicsUtility.SOEBrushes.ControlBorder)));
                        catch (ArgumentOutOfRangeException ex)
                            // The second time we get an unexpected Index out of bounds here, but it still adds the palette.
                Telerik.Windows.Controls.CollectionExtensions.AddRange(this.viewModel.ShiftTypes, data);


  2. Pavel R. Pavlov
    Pavel R. Pavlov avatar
    1236 posts

    Posted 19 Nov 2014 Link to this post

    Hello Håkan,

    I cannot be completely sure about the reason behind the reported issue since I do not have your code. However, it is important to keep in mind that you should first initialize the Palette before filling it with PaletteEntry objects. Please take a look at the attached project and let me know if you have any further questions.

    Pavel R. Pavlov

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

  3. Håkan
    Håkan avatar
    183 posts
    Member since:
    May 2011

    Posted 20 Nov 2014 in reply to Pavel R. Pavlov Link to this post


    I have initialized it in XAML like this:


    Then on every refresh i clear the GlobalEntries collection before adding new.

    I tried to set it in code instead as in your project but with no difference.

Back to Top