Search code examples
wpfvb.netdata-bindingobservablecollectionwpfdatagrid

Datagrid values not updating in wpf vb.net when using databinding on an observablecollection


I'm using databinding in a datagrid, and I can get the values into the grid, but when I edit them, it's not firing the update of the values in the property for the value. I want to be able to do something with the value when I have changed it.

In my PLPage.xaml.vb I have:

Class PLPage
Implements INotifyPropertyChanged

Public MyCollection As New ObservableCollection(Of Pl)

Dim reg As List(Of String) = New List(Of String)()

Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged

Public Sub New()
    InitializeComponent()
    Me.DataContext = MyCollection

End Sub

Private Sub FillData()
Dim pla As Pl = New Pl()
            pla.Reg = reg(count)

            MyCollection.Add(pla)


        PGTable.ItemsSource = MyCollection
End Sub

Public Class Pl
    Implements INotifyPropertyChanged
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged

Public Property _r As String

Public Property Reg As String
    Get
        Return _r
    End Get
    Set(value As String)
        If _r <> value Then
            _r = value
            'RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("Reg"))
            NotifyPropertyChanged("Reg")
        End If
    End Set
End Property

Private Sub NotifyPropertyChanged(propertyName As String)
    Me.OnPropertyChanged(New PropertyChangedEventArgs(propertyName))
End Sub

Protected Overridable Sub OnPropertyChanged(e As PropertyChangedEventArgs)
    RaiseEvent PropertyChanged(Me, e)
End Sub

End Class

Then in my xaml I have:

<DataGrid ItemsSource="{Binding Pl}" x:Name="PlGridTable" AutoGenerateColumns="False">
                        <DataGrid.Columns>

                            <materialDesign:MaterialDataGridTextColumn Header="Desig" Width="Auto" Binding="{Binding _reg, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" EditingElementStyle="{StaticResource MaterialDesignDataGridTextColumnPopupEditingStyle}"/>

                        </DataGrid.Columns>
                    </DataGrid>

When I first load it up, it is hitting the reg property stuff, and assigning the value correctly, but when I update the value using the editing element built into the material design package, it's not firing off anywhere. Any help as to why would be appreciated.


Solution

  • {Binding _reg} should be {Binding Reg} since the property is named Reg:

    <materialDesign:MaterialDataGridTextColumn Header="Desig" Width="Auto" Binding="{Binding Reg, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" EditingElementStyle="{StaticResource MaterialDesignDataGridTextColumnPopupEditingStyle}"/>