質問

I have some controls and I'm using ObservableCollection (OC) to bind some info to them. Although, I am sure I am doing this completely wrong, because everytime I need an "update" I just rebind the new OC to the DataContext of my control

MyControl.DataContext = ObservableDocument.GetAllDocuments(); 
//GetAllDocuments returns a new OC with ObservableDocument

In addition to the fact that this is just the wrong way to do it, the problem is, that when the amount of data gets big, performance starts to drop really hard.

So I decided to make the collection static and update it. But now I've hit a huge rock.

What's the right way to update an ObservableCollection if I'm using EntityFramework to get the data? Should I even use the OC if I'm using Entity Framework?

It just seems dumb to write a custom Equals for every class collection that I need to update.

P.S. All the classes that I use with ObservableCollection already implement the INotifyOnPropertyChange interface.

役に立ちましたか?

解決

You have to get a collectionviewsource bound to your control and then update the collection view on page load with your data collection. Every time you update, refresh the collectionviewsource. Following is a fragment of code to understand what I have said here. This code is to populate a DataGrid. Same binding pattern can be used to update a single record where the INotifyPropertyChanged comes in to play; Hope it is clear enough.

XAML:

 <Page.Resources>
    <CollectionViewSource x:Key="TestMySource" ..... />
</Page.Resources>

 <Grid>
   <StackPanel >
     <DataGrid x:Name="MyGrid" ItemsSource="{Binding}" DataContext="{StaticResource TestMySource}">
         <!-- grid--->
     </DataGrid>
   </StackPanel>
  <Grid>

Code:

Dim mytestsource As System.Windows.Data.CollectionViewSource
Dim mylist As ObjectModel.ObservableCollection(Of SimpleClass)

 Private Sub pgWorkwithDocumentTypes_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
    Me.mytestsource = CType(Me.Resources("TestMySource"), System.Windows.Data.CollectionViewSource)
    mylist = boTest.Get_List <!--your method to get data-->
    Me.mytestsource.Source = mylist       
End Sub
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top