I am relatively new to WPF and the MVVM model, but after hours of searching and reading articles regarding the matter, I have not come up with a definitive solution to this.
1) Model: I have an EF model relying on an SQL Express database. For this example, I will use a property named Visits, a DbSet(Of Visit).
2) ViewModel: an ObservableCollection(Of Visit). It is declared as such:
Private _Visits As ObservableCollection(Of Visit)
Public Property Visits As ObservableCollection(Of Visit)
Get
Return _Visits
End Get
Set(value As ObservableCollection(Of Visit))
_Visits = value
RaisePropertyChanged("Visits")
End Set
End Property
...
Visits = New ObservableCollection(Of Visit)(
From V In Application.context.Visits
Where V.IsRobotGenerated AndAlso
Not V.IsSynced
)
3) View: a WPF datagrid.
<DataGrid
x:Name="grdVisits"
ItemsSource="{
Binding Path=Visits,
RelativeSource={RelativeSource FindAncestor, AncestorType=Window}
}"
/>
Now I have a background thread which modifies the EF data directly (does all CRUD operations). What I want is the datagrid to reflect these changes as they happen.
What I am doing right now is replace the Visits ObservableCollection by re-calling the constructor above, which results to 2 drawbacks:
1) Performance wise I suppose it is not the best choice, especially when we are talking about quite a few thousands of rows (each with its RowDetails).
2) The user has in the meantime scrolled down the datagrid or opened a RowDetailsTemplate. His position and everything is lost if the entire collection is reset.
I have read somewhere about DependencyProperties but I do not have time to go through it thoroughly since I have no clue if it is a pointer to the right direction. If it is, I will be happy to do so.
Any help will be greatly appreciated.