I'm trying to use databinding in WPF with an MVVM pattern. So far, it seems that everything is plumbed up properly and I'm using the right type of classes and objects. My observable collection loads to first time correctly, but won't refresh on the datagrid when it changes. Observable Collections should automatically implement INPC and I've also provided a handler for it in my base class, so I'm confused why it is still not working and updating my UI. Here's my code below:
ViewModelBase Class:
<!-- language: lang-vb -->#Region "INotifyPropertyChanged Members"
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Protected Overridable Sub OnPropertyChanged(ByVal propertyName As String)
VerifyPropertyName(propertyName)
Dim handler As PropertyChangedEventHandler = PropertyChangedEvent
If handler IsNot Nothing Then
Dim e = New PropertyChangedEventArgs(propertyName)
handler(Me, e)
End If
End Sub
#End Region
MainWindowViewModel Class:
<!-- language: lang-vb -->Public Class MainWindowViewModel
Inherits ViewModelBase
Private Shared _coreInfoData As ObservableCollection(Of Person)
Public Shared Property CoreInfoData() As ObservableCollection(Of Person)
Get
Return _coreInfoData
End Get
Set(ByVal value As ObservableCollection(Of Person))
_coreInfoData = value
End Set
Public Sub Main()
If CoreInfoData IsNot Nothing Then
CoreInfoData.Clear()
End If
CoreInfoData = GetRecords()
End Sub
The GetRecords function is a call to a database that gets the most recent set of records available.
In my view, I'm actually binding to a CollectionViewSource and binding that to a datagrid:
<!-- language: lang-xaml --><UserControl.Resources>
<CollectionViewSource Source="{Binding Path=CoreInfoData, Mode=TwoWay}" x:Key="cvs">
<CollectionViewSource.GroupDescriptions>
<dat:PropertyGroupDescription PropertyName="GroupId"/>
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
</UserControl.Resources>
<DataGrid ItemsSource="{Binding Source={StaticResource cvs}}" AutoGenerateColumns="False" >
<DataGrid.Columns>
<DataGridTextColumn Header="Last Name" Binding="{Binding Path=LastName}" IsReadOnly="True" />
<DataGridTextColumn Header="First Name" Binding="{Binding Path=FirstName}" IsReadOnly="True"/>
</DataGrid.Columns>
</DataGrid>
The Person
class has properties LastName
and FirstName
(amoung others). As I understand it, if the ObservableCollection itself changes - like what I do by clearning and filling it back up, it should automatically fire the INPC event, but my DataGrid doesn't seem to refresh the second time around. Any ideas would be really helpful.
Thanks!
EDIT Here's how I wound up solving the problem:
Public Shared Property CoreInfoData() As ObservableCollection(Of Person)
<!-- language: lang-vb -->Public Shared Property CoreInfoData() As ObservableCollection(Of Person)
Get
Return _coreInfoData
End Get
Set(ByVal value As ObservableCollection(Of Person))
If _coreInfoData Is Nothing Then
'get data
_coreInfoData = value
Else
'refresh data
_coreInfoData.Clear()
For i = 0 To value.Count - 1
_coreInfoData.Add(value(i))
Next
End If
End Set
End Property
The First time the Set Method is called it will create a new _coreInfoData variable. Subsequent calls will replace the existing one by clear it's entire contents and looping through each item to add it back into the collection. Thanks to Clemens, Dan Busha, and Charleh (and others) for their helpful comments!