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:
#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:
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:
<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)
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!
No PropertyChanged event will be raised automatically simply because the type of a property is ObservableCollection
. You would have to raise it in the setter:
Set(ByVal value As ObservableCollection(Of Person))
_coreInfoData = value
OnPropertyChanged("CoreInfoData")
End Set
It looks like you're assigning a value to your ObservableCollection which doesn't change your collection it replaces it. Either raise a PropertyChanged event for for the CoreInfoData property or add the items one by one from the set
value to the ObservableCollection (the ObservableCollection should manage the raising of the PropertyChanged event for you).
You are confusing INotifyPropertyChanged with INotifyCollectionChanged .
ObservableCollection does implement INotifyPropertyChanged, this is used to fire the event for properties like Count and the indexer Item[]. It does not extend to the functionality you require it to achieve.
first - why is your collection Shared/static?
when working with ObservableColelction then usually you create a instance just once and use clear, add remove.
if you wanna set a new instance like you did in your setter you have to call OnPropertyChanged("CoreInfoData") too.
but i would prefer clear and add
CoreInfoData.Clear()
CoreInfoData.AddRange(GetRecords())
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.