question

MarcJeeves-9615 avatar image
0 Votes"
MarcJeeves-9615 asked ·

This type of CollectionView does not support changes to its SourceCollection from a thread different from the Dispatcher thread.

Morning All,

The problem I'm having is if i add or remove items from the "private static ObservableCollection ContinentModel _ContinentModels;" xaml crashes, so i changed it around a bit and i now get this error

This type of CollectionView does not support changes to its SourceCollection from a thread different from the Dispatcher thread.

how do i update the collection?

Thanks

Madaxe


//------------------------------------------------------------------mainwindow.xaml

                     <DataGrid Name="DGD_UserList" SelectionChanged="DGD_UserList_SelectionChanged"
                               AutoGenerateColumns="False"  
                               ItemsSource="{Binding ContinentModels, 
                                             Mode=OneWay, 
                                             NotifyOnSourceUpdated=True, 
                                             UpdateSourceTrigger=PropertyChanged}" 
                               Margin="300,10,-0.2,1.8" 
                               ColumnWidth="*" 
                               Background="#FFFFECEC">
                         <DataGrid.Columns>
                             <DataGridTextColumn Binding="{Binding objectID}" Header="Object Id" IsReadOnly="True"/>
                             <DataGridTextColumn Binding="{Binding continentName}" Header="Continent" IsReadOnly="True"/>
                         </DataGrid.Columns>
                         <DataGrid.ContextMenu>
                             <ContextMenu>
                                 <MenuItem Name ="CMN_DeleteContinent" Header="Delete Continent" Click="CMN_DeleteContinent_Click"/>
                             </ContextMenu>
                         </DataGrid.ContextMenu>
                     </DataGrid>

//------------------------------------------------------------------xaml.cs

         public MainWindow()
         {
             InitializeComponent();
             DataContext = _CustomDataImplementation;
         }

//------------------------------------------------------------------implementation

 using InfarstructureProject.CustomTypes;
 using InfarstructureProject.Models;
 using InfrastructureProject.XML;
    
 using System;
 using System.ComponentModel;
 using System.Net.Http;
 using System.Runtime.CompilerServices;
 using System.Threading.Tasks;
    
 namespace ImplementationProject.Implementation
 {
     public class CustomDataImplementation:INotifyPropertyChanged
     {
         public event PropertyChangedEventHandler PropertyChanged;
    
         private static string _RootUrl = @"https://xxxxxxxx.execute-api.xxxxx-1.amazonaws.com/xxxxx/";
         private AsyncObservableCollection<ContinentModel> _ContinentModels = null;
    
         public AsyncObservableCollection<ContinentModel> ContinentModels
         {
             get { return _ContinentModels; }
             set
             {
                 _ContinentModels = value;
                 OnPropertyChanged();
             }
         }
         public CustomDataImplementation()
         {
             this.ContinentModels = this.GetContinentData();
         }
    
         #region "Read"
         private AsyncObservableCollection<ContinentModel> GetContinentData()
         {
             AsyncObservableCollection<ContinentModel> ReturnContinentModles = null;
             Task<AsyncObservableCollection<ContinentModel>> ContinentTask = this.GetContinentModels();
             ContinentTask.Wait();
    
             if (ContinentTask.Result.Count > 0)
             {
                 ReturnContinentModles = ContinentTask.Result;
             }
             return ReturnContinentModles;
         }
         private async Task<AsyncObservableCollection<ContinentModel>> GetContinentModels()
         {
             AsyncObservableCollection<ContinentModel> ReturnContinentModels = null;
             try
             {
                 using (HttpClient httpClient = new HttpClient())
                 {
                     httpClient.DefaultRequestHeaders.Add("Accept", "application/json;charset=UTF-8");
                        
                     using (HttpResponseMessage HttpResponseMessage = await httpClient.GetAsync(string.Concat( _RootUrl, "v1/xxxxContinent/All"), HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false))
                     {
                         if(HttpResponseMessage.StatusCode == System.Net.HttpStatusCode.OK)
                         {
                             using (HttpContent HttpContent = HttpResponseMessage.Content)
                             {
                                 string MyContent = await HttpContent.ReadAsStringAsync();
                                 ReturnContinentModels = XMLHelper.DeserializeObject<AsyncObservableCollection<ContinentModel>>(MyContent);
                             }
                         }
                     }
                 }
             }
             catch (Exception ex)
             {
                 Console.WriteLine(ex.Message); 
             }
             return ReturnContinentModels;
         }
         #endregion
    
         #region "Create"
         public void CreateContinent(string iContinentName)
         {
             this.CreateNewContinent(iContinentName);
         }
         private async void CreateNewContinent(string iContinentName)
         {
             string data = "";
             try
             {
                 using (HttpClient httpClient = new HttpClient())
                 {
                     httpClient.DefaultRequestHeaders.Add("Accept", "application/json;charset=UTF-8");
    
                     using (HttpResponseMessage HttpResponseMessage = await httpClient.PostAsync(string.Concat(_RootUrl, "V1/xxxxContinent/" + iContinentName), new StringContent(data)).ConfigureAwait(false))
                     {
                         if (HttpResponseMessage.StatusCode == System.Net.HttpStatusCode.OK)
                         {
                             using (HttpContent HttpContent = HttpResponseMessage.Content)
                             {
                                 string MyContent = await HttpContent.ReadAsStringAsync();
                                 ContinentModel NewContinentModel = XMLHelper.DeserializeSimpleObject<ContinentModel>(MyContent);
    
                                 AsyncObservableCollection<ContinentModel> NewContinentModels = _ContinentModels;
                                 NewContinentModels.Add(NewContinentModel);
                                 this.ContinentModels = NewContinentModels;
                             }
                         }
                     }
                 }
             }
             catch (Exception ex)
             {
                 Console.WriteLine(ex.Message);
             }
         }
         #endregion
    
         protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
         {
             PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
         }
     }
 }





windows-wpf
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

0 Answers