Note that there are some explanatory texts on larger screens.

plurals
  1. POListbox using a data template and dynamic data loading based on selection Enumeration Error
    primarykey
    data
    text
    <p>To anyone that can help or show me a better way. I have tried to do this with an observable collection, a list based on a custom class, global/non-global collections, using the listbox's itemssource, synclocking, and finally emptying and manually entering in items.</p> <p>It tends to work fine, but every once in a while I get the error "Collection was modified; enumeration operation may not execute." which seems to occur at System.Collections.ArrayList.ArrayListEnumeratorSimple.MoveNext()</p> <p>I have tried a few different things over the past couple of weeks and I am able to avoid it sometimes, but then I can reproduce it in a different area.</p> <p>Essentially, I have three listboxes Parent, Current, Children. These have captioned images in them. When an Image is selected the images in the Parent, Current, and Children empty and reload based on the selected image.</p> <p>I've written it in vb, but I can convert it to C# if that will help. The code has been changed many times and so the latest one has a lot of commented code in it. Any Help or suggestions would be greatly appreciated. Anything to simplify it, get it to work, or performance enhancements would be great.</p> <p>Imagine the code in triplicate. The code is nearly identical with the exception of names.</p> <pre><code>&lt;Page.Resources&gt; &lt;CollectionViewSource Source="{Binding Source={x:Static Application.Current}, Path=CurrentList}" x:Key="CurrentList" /&gt; &lt;/Page.Resources&gt; &lt;ScrollViewer Grid.Row="2" VerticalScrollBarVisibility="Disabled" PanningMode="VerticalOnly" FlowDirection="RightToLeft" HorizontalScrollBarVisibility="Auto" &gt; &lt;ListBox Name="CurrentListbox" VerticalAlignment ="Stretch" Margin=" 8" Background="Transparent" Height="Auto" BorderThickness="0" HorizontalContentAlignment="Center" FlowDirection="LeftToRight" HorizontalAlignment="Center" ItemsSource="{Binding CurrentList}"&gt; &lt;ListBox.Resources&gt; &lt;Style TargetType="{x:Type ListBoxItem}"&gt; &lt;EventSetter Event="ListBoxItem.Selected" Handler="CurrentListBoxItem_Selected" HandledEventsToo="false"/&gt; &lt;/Style&gt; &lt;/ListBox.Resources&gt; &lt;ListBox.ItemsPanel&gt; &lt;ItemsPanelTemplate&gt; &lt;WrapPanel IsItemsHost="True" /&gt; &lt;/ItemsPanelTemplate&gt; &lt;/ListBox.ItemsPanel&gt; &lt;ListBox.ItemTemplate&gt; &lt;DataTemplate&gt; &lt;Border BorderBrush="#FF0000AC" Style="{StaticResource ResourceKey=ThumbnailBorder}"&gt; &lt;!--&lt;Viewbox MaxWidth ="100" &gt;--&gt; &lt;StackPanel&gt; &lt;Grid&gt; &lt;Border Name="ItemBorder" Style="{StaticResource ResourceKey=ThumbnailInnerBorder}"/&gt; &lt;Image Name="PersonImage" MaxHeight ="200" MaxWidth ="200" Source="{Binding ProfileImagePath}" HorizontalAlignment="Center" &gt; &lt;Image.OpacityMask&gt; &lt;VisualBrush Visual="{Binding ElementName=ItemBorder}"/&gt; &lt;/Image.OpacityMask&gt; &lt;/Image&gt; &lt;/Grid&gt; &lt;Viewbox MaxWidth ="190" Margin="5,0,5,0" MaxHeight="15"&gt; &lt;TextBlock Name="Person" Text="{Binding ProfileName}" Tag="{Binding UserID}" HorizontalAlignment="Center" /&gt; &lt;/Viewbox&gt; &lt;/StackPanel&gt; &lt;!--&lt;/Viewbox&gt;--&gt; &lt;/Border&gt; &lt;/DataTemplate&gt; &lt;/ListBox.ItemTemplate&gt; &lt;/ListBox&gt; &lt;/ScrollViewer&gt; </code></pre> <p>The Collection is stored in a global Variable and populated using a SQlCe Command.</p> <pre><code>Public Shared CurrentPeopleList As New CollectionViewSource Public Shared Current_People_List As New ObservableCollection(Of Currents)() Public Shared Property CurrentList() As ObservableCollection(Of Currents) Get Return Current_People_List End Get Set(ByVal value As ObservableCollection(Of Currents)) Current_People_List = value End Set End Property </code></pre> <p>The Custom Class of Currents:</p> <pre><code>Public Class Currents Implements INotifyPropertyChanged Private ProfileNameValue As String Private UserIDValue As Integer Private ProfileImagePathValue As String Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged </code></pre> <p>#Region "Properties Getters and Setters" Public Property ProfileName() As String Get Return Me.ProfileNameValue End Get Set(ByVal value As String) Me.ProfileNameValue = value OnPropertyChanged("ProfileName") End Set End Property</p> <pre><code>Public Property UserID() As Integer Get Return Me.UserIDValue End Get Set(ByVal value As Integer) If value &lt; 0 Then Throw New ArgumentException("User ID must be greater than 0 ") End If Me.UserIDValue = value OnPropertyChanged("UserID") End Set End Property Public Property ProfileImagePath() As String Get Return Me.ProfileImagePathValue End Get Set(ByVal value As String) Me.ProfileImagePathValue = RelativeProgramPath() &amp; "Media\Pictures\" &amp; value OnPropertyChanged("ProfileImagePath") End Set End Property #End Region Public Sub New(ByVal UserID As Integer, ByVal ProfileName As String, ByVal ProfileImagePath As String) Me.ProfileNameValue = ProfileName Me.UserIDValue = UserID Me.ProfileImagePathValue = If(ProfileImagePath Like "pack://*", ProfileImagePath, RelativeProgramPath() &amp; "Media\Pictures\" &amp; ProfileImagePath) End Sub Protected Sub OnPropertyChanged(ByVal name As String) RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(name)) End Sub End Class </code></pre> <p>Finally, the way these are populated is by using a common function</p> <pre><code> Private Sub Reload(ByVal CurrentID As Integer) Try Try ParentList = Nothing CurrentList = Nothing ChildrenList = Nothing Catch End Try Try Me.ParentListbox.ItemsSource = Nothing Me.CurrentListbox.ItemsSource = Nothing Me.ChildListbox.ItemsSource = Nothing Catch End Try Try CurrentPersonID = CurrentID 'Load the Images Based on the people. Dim Ch = Load_Children_People(CurrentID) Dim C = Load_Current_People(CurrentID) Dim P = Load_Parent_People(CurrentID) ParentList = If(P.Count &gt; 0, P, Nothing) CurrentList = If(C.Count &gt; 0, C, Nothing) ChildrenList = If(Ch.Count &gt; 0, Ch, Nothing) Catch End Try Try If ParentList IsNot Nothing Then _ Me.ParentListbox.ItemsSource = CollectionViewSource.GetDefaultView(ParentList) If CurrentList IsNot Nothing Then _ Me.CurrentListbox.ItemsSource = CollectionViewSource.GetDefaultView(CurrentList) If ChildrenList IsNot Nothing Then _ Me.ChildListbox.ItemsSource = CollectionViewSource.GetDefaultView(ChildrenList) Catch End Try Catch End Try 'For some reason this will not work without a pause. Not sure why. 'FIXME Remove Pause Delay When able. 'Thread.Sleep(200) End Sub </code></pre> <p>I used a lot of try-catch's to attempt to catch the error, but have been unsuccessful thus far. My suspicion is that the error occurs during a UI thread that I cannot seem to pin down. As I mentioned earlier. any assistance would be great.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload