Forums

InvalidOperationException "Collection was modified"
Last Post 11 Feb 2018 04:53 PM by James Haywood. 2 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
James HaywoodUser is Offline
New Member
New Member
Posts:13


--
11 Feb 2018 07:38 AM
    I've searched the forums and it looks like this has been hit by other people too. I'm not sure what I'm doing wrong, but sometimes trying to bind a control's property to a value in my ViewModel results in an InvalidOperationException with the message, "Collection was modified; enumeration operation may not execute."

    I'm not doing anything crazy, just creating a ViewModel and assigning it to a View, like this...

    var titleVM = new TitleVM();
    var titleUI = new TitleUI() { DataContext = titleVM };

    It's only happened with Brush values so far. It's happened trying to bind the Foreground of the UIRoot, and with the BorderBrush of a Border. It's odd because I'm able to bind the BorderBrush of other Borders in another UIRoot without issue. Although, I do get the exception binding the Foreground in that one too.

    If I don't try binding the colors and set them directly in the xaml file with one of the standard color strings, it works fine. No exception is thrown.

    Here's one of the stack traces from the point at which I'm newing up the UI and assigning the DataContext...

    System.InvalidOperationException
    HResult=0x80131509
    Message=Collection was modified; enumeration operation may not execute.
    Source=mscorlib
    StackTrace:
    at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
    at System.Collections.Generic.List`1.Enumerator.MoveNextRare()
    at System.Collections.Generic.List`1.Enumerator.MoveNext()
    at EmptyKeys.UserInterface.Media.VisualTreeHelper.PropagateInheritanceChange(DependencyObject 00389*/, DependencyProperty 0038A*/, /\*„ 0038B*/, Boolean 0038C*/, DependencyPropertyChangedEventArgs 0038D*/)
    at EmptyKeys.UserInterface.Media.VisualTreeHelper.PropagateInheritanceChange(DependencyObject 00389*/, DependencyProperty 0038A*/, /\*„ 0038B*/, Boolean 0038C*/, DependencyPropertyChangedEventArgs 0038D*/)
    at EmptyKeys.UserInterface.Media.VisualTreeHelper.PropagateInheritanceChange(DependencyObject 00389*/, DependencyProperty 0038A*/, /\*„ 0038B*/, Boolean 0038C*/, DependencyPropertyChangedEventArgs 0038D*/)
    at EmptyKeys.UserInterface.Media.VisualTreeHelper.PropagateInheritanceChange(DependencyObject 00389*/, DependencyProperty 0038A*/, /\*„ 0038B*/, Boolean 0038C*/, DependencyPropertyChangedEventArgs 0038D*/)
    at EmptyKeys.UserInterface.Media.VisualTreeHelper.PropagateInheritanceChange(DependencyObject 00389*/, DependencyProperty 0038A*/, /\*„ 0038B*/, Boolean 0038C*/, DependencyPropertyChangedEventArgs 0038D*/)
    at EmptyKeys.UserInterface.Media.VisualTreeHelper.PropagateInheritanceChange(DependencyObject 00389*/, DependencyProperty 0038A*/, /\*„ 0038B*/, Boolean 0038C*/, DependencyPropertyChangedEventArgs 0038D*/)
    at EmptyKeys.UserInterface.Media.VisualTreeHelper.PropagateInheritanceChange(DependencyObject 00389*/, DependencyProperty 0038A*/, /\*„ 0038B*/, Boolean 0038C*/, DependencyPropertyChangedEventArgs 0038D*/)
    at EmptyKeys.UserInterface.UIElement./*ȧ(/\*„ 00228*/, DependencyPropertyChangedEventArgs 00229*/)
    at EmptyKeys.UserInterface.UIElement./*Ƌ(DependencyObject 0018C*/, DependencyPropertyChangedEventArgs 0018D*/)
    at EmptyKeys.UserInterface.DependencyObject.SetValueInternal(DependencyProperty 000DF*/, Object 000E0*/, /\*B 000E1*/)
    at EmptyKeys.UserInterface.DependencyObject.SetValue(DependencyProperty property, Object value)
    at EmptyKeys.UserInterface.UIElement.set_DataContext(Object value)
    Filip DušekUser is Offline
    Advanced Member
    Advanced Member
    Posts:695


    --
    11 Feb 2018 01:57 PM
    Hi, this is multi threading issue. Be sure that you are calling UpdateLayout method on the same thread like the rest of your code, which is setting something in the UI.
    James HaywoodUser is Offline
    New Member
    New Member
    Posts:13


    --
    11 Feb 2018 04:53 PM
    Hmm. My game is not multithreaded. I double checked by setting breakpoints in the create and update calls and using the Threads debug window in Visual Studio. Everything is happening on the main thread. In the stack I posted, 'PropagateInheritanceChange' is being called multiple times. Not knowing what that does exactly, is it possible that an inherited property is being added to a collection while it's being looped through?

    If it helps, my project is on Bitbucket, and I planned on making it public at some point so I can do that sooner rather than later if you want to take a look at it.
    You are not authorized to post a reply.