在可观察性集合上使用数据绑定时,wpf vb.net中的Datagrid值未更新

时间:2018-11-06 07:21:59

标签: wpf vb.net data-binding observablecollection wpfdatagrid

我在数据网格中使用数据绑定,并且可以将值放入网格中,但是当我编辑它们时,它不会触发值属性中值的更新。我希望能够在更改值后对其进行处理。

在我的PLPage.xaml.vb中,我有:

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

然后在我的xaml中,我拥有:

<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>

当我第一次加载它时,它会碰到reg属性,并正确分配了值,但是当我使用材料设计包中内置的编辑元素更新值时,它不会在任何地方触发。任何有关为什么的帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

{Binding _reg}应该为{Binding Reg},因为该属性的名称为Reg

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