ChartBehaviors property using MVVM pattern

Hello,
I want to control the ChartBehaviors property of my Charts, i'm binding the property to a ChartBehaviorCollection from my ViewModel but running the app i'm getting "System.InvalidOperationException: 'Collection was modified; enumeration operation may not execute.'". The error occurs adding a ChartBehavior object (ChartZoomPanBehavior or ChartTooltipBehavior) to the ChartBehaviorCollection. How can i control this property without getting the error?.

6 Replies 1 reply marked as answer

DD Devakumar Dhanapoosanam Syncfusion Team June 2, 2020 07:57 AM UTC

Hi Juan Guerrero, 
 
Greetings from Syncfusion. 
 
We have analyzed your query and we would like to you know that the reported error occurs when changing the collection dynamically while reading it.  
 
Can you please share the following details which will be helpful for us to analyze the reported issue and provide a better solution at earlier? 
·       Can you please share the chart related code snippet details and adding ChartBehavior collection code snippet? 
·       Can you please ensure us whether you are modifying the collection values value reading it using a foreach or not? 
·       Could you please share the stack trace details if possible? 
·       Please share the use case of your application. 
 
Please find the tested sample below, 
 
 
Please let us know if you need any further assistance on this. 
 
Regards, 
Devakumar D 



JG Juan Guerrero June 2, 2020 03:28 PM UTC

The app it's designed to present multiple sales charts into a rotator dynamically, for example some charts need to enable zooming but some others don't.
I have a model from my charts, every chart are into a SfRotator and i want to control the behaviors for each chart so the model contains a Behaviors(ChartBehaviorCollection) property:
Here's the rotator item model:

    public class RotatorModel: INotifyPropertyChanged
    {
        //Some full properties
        private ChartBehaviorCollection _chartBehavior;

        public ChartBehaviorCollection Behaviors
        {
            get
            {
                return _chartBehavior;
            }
            set
            {
                if (_chartBehavior != value)
                {
                    _chartBehavior = value;
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Behaviors)));
                }
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
    }

Here's the code where i'm trying to add the behaviors for each model:

        private ObservableCollection<RotatorModel> _rotatorElements;

        public ObservableCollection<RotatorModel> RotatorElements
        {
            get 
            { 
                return _rotatorElements; 
            }
            set 
            {
                if (_rotatorElements != value)
                {
                    _rotatorElements = value;
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(RotatorElements)));
                }
            }
        }

public async Task LoadSales()
{
     RotatorElements = new ObservableCollection<RotatorModel>();
     //some code to get the data
     foreach (var graph in graphData.Graphs)
     {
                                                SfChart chart = new SfChart();
                                                chart.Title = new ChartTitle() { Text = graph.GraphLabel };

                                                //Chart behaviors
                                                ChartZoomPanBehavior chartZoomPanBehavior = new ChartZoomPanBehavior();
                                                chartZoomPanBehavior.EnableZooming = true;
                                                chartZoomPanBehavior.EnableDoubleTap = true;
                                                ChartTooltipBehavior chartTooltipBehavior = new ChartTooltipBehavior();
                                                chartTooltipBehavior.BackgroundColor = Color.Red;
                                                chartTooltipBehavior.BorderWidth = 1;
                                                chartTooltipBehavior.BorderColor = Color.Black;
                                                chartTooltipBehavior.TextColor = Color.White;
                                                chartTooltipBehavior.Duration = 2;
                                                chart.ChartBehaviors.Add(chartTooltipBehavior);
                                                chart.ChartBehaviors.Add(chartZoomPanBehavior);

                                                //some code

                                                RotatorModel model = new RotatorModel();
                                                model.Series = chart.Series;
                                                model.Title = chart.Title;
                                                model.PrimaryAxis = primaryAxis;
                                                model.SecondaryAxis = secondaryAxis;
                                                model.Behaviors = chart.ChartBehaviors;
                                                RotatorElements.Add(model);
     }
}

and here's the xaml:

                                        <syncfusion1:SfRotator
                                                EnableLooping="False" 
                                                EnableSwiping="True"
                                                ItemsSource="{Binding RotatorElements}"
                                                NavigationDirection="Horizontal" 
                                                NavigationStripMode="Dots" 
                                                NavigationStripPosition="Bottom">
                                            <syncfusion1:SfRotator.ItemTemplate>
                                                <DataTemplate>
                                                    <chart:SfChart
                                                        ChartBehaviors="{Binding Behaviors}"
                                                        Series="{Binding Series}" 
                                                        Title="{Binding Title}" 
                                                        PrimaryAxis="{Binding PrimaryAxis}" 
                                                        SecondaryAxis="{Binding SecondaryAxis}">
                                                        <chart:SfChart.ColorModel>
                                                            <chart:ChartColorModel 
                                                                Palette="Custom" 
                                                                CustomBrushes="{StaticResource Colors1}"/>
                                                        </chart:SfChart.ColorModel>
                                                    </chart:SfChart>
                                                </DataTemplate>
                                            </syncfusion1:SfRotator.ItemTemplate>
                                        </syncfusion1:SfRotator>



DD Devakumar Dhanapoosanam Syncfusion Team June 3, 2020 01:21 PM UTC

Hi Juan Guerrero, 
 
Thanks for your update. 
 
Currently we are validating the reported issue at our end based on the provided code snippet and we will update you the status on or before June 5, 2020. 
 
Regards, 
Devakumar D 



DD Devakumar Dhanapoosanam Syncfusion Team June 5, 2020 12:14 PM UTC

Hi Juan Guerrero, 
 
We have confirmed the reported issue as a bug and logged a defect report. The fix for this issue will be included in our upcoming weekly NuGet release which is expected to be rolled on June 16, 2020. 
 
     
If you have any more specification/precise replication procedure or a scenario to be tested, you can add it as a comment in the portal link above.   
    
We appreciate your patience until then. 
 
Regards, 
Devakumar D 



DD Devakumar Dhanapoosanam Syncfusion Team June 16, 2020 03:56 PM UTC

Hi Juan Guerrero, 
 
Due to some technical hurdles, we were unable to move fix into our today weekly NuGet. So, currently we are working on this with high priority and we will provide a fix in our next weekly NuGet release on June 23, 2020. 
 
Regards, 
Devakumar D 



DD Devakumar Dhanapoosanam Syncfusion Team June 24, 2020 04:09 AM UTC

Hi Juan Guerrero, 
 
We are glad to announce that our weekly NuGet was rolled out and fix for the reported issue was included in the weekly NuGet. 
 
NuGet Version: 18.1.0.59 
 
Please check and let us know if you have any concern on this. 
 
Regards, 
Devakumar D 


Marked as answer
Loader.
Up arrow icon