WPF DataGrid: Synchronisation mit Datenquelle

  • WPF

Es gibt 1 Antwort in diesem Thema. Der letzte Beitrag () ist von MarioD.

    WPF DataGrid: Synchronisation mit Datenquelle

    Hallo,

    ich habe ein Problem mit der Synchronisation zwischen DataGrid und seiner Datenquelle bei Bearbeitung der Daten im View.
    Ändere ich im View die Daten meines DataGrids und lasse mir danach in der Methode
    dataGrid_RowEditEnding die Daten seiner korrespondierenden Datenquelle (eine ObservableCollection) ausgeben, sind die Datensätze im View und ViewModel nicht gleich.

    Erst nach Focus-Verlust des DataGrids sind beide Listen wieder synchron.
    Wie kann ich denn veranlassen, dass Änderungen im View sofort ins ViewModel übernommen werden?

    Hier mein Code:

    VB.NET-Quellcode

    1. Imports System.Windows
    2. Imports System.Diagnostics
    3. Namespace MVVMProject
    4. Public Partial Class MainWindow
    5. Inherits Window
    6. Private vm As New BillViewModel()
    7. Public Sub New()
    8. InitializeComponent()
    9. vm.addPosition(New Position(1, 111, 1.5, 10.45))
    10. vm.addPosition(New Position(2, 222, 2.5, 20.45))
    11. vm.addPosition(New Position(3, 333, 3.5, 30.45))
    12. DataContext = vm
    13. End Sub
    14. 'Liefert nach Bearbeitung im View nicht die neuen Daten
    15. Private Sub dataGrid_RowEditEnding(sender As Object, e As System.Windows.Controls.DataGridRowEditEndingEventArgs)
    16. For Each item As Position In vm.PositionList
    17. Debug.WriteLine(item.ToString())
    18. Next
    19. End Sub
    20. End Class
    21. Public Class BillViewModel
    22. Inherits BaseViewModell
    23. Private _bill As Bill
    24. Public Sub New()
    25. _bill = New Bill()
    26. End Sub
    27. Public Sub New(_bill As Bill)
    28. Me.Bill = _bill
    29. End Sub
    30. Public Property Bill() As Bill
    31. Get
    32. Return _bill
    33. End Get
    34. Set
    35. _bill = value
    36. OnPropertyChanged("Bill")
    37. End Set
    38. End Property
    39. Public Property BillId() As Integer
    40. Get
    41. Return _bill._billId
    42. End Get
    43. Set
    44. _bill._billId = value
    45. OnPropertyChanged("BillId")
    46. End Set
    47. End Property
    48. Public Property [Date]() As DateTime
    49. Get
    50. Return Bill._date
    51. End Get
    52. Set
    53. Bill._date = value
    54. OnPropertyChanged("Date")
    55. End Set
    56. End Property
    57. Public Property PositionList() As ObservableCollection(Of Position)
    58. Get
    59. Return Bill._positions
    60. End Get
    61. Set
    62. Bill._positions = value
    63. OnPropertyChanged("PositionList")
    64. End Set
    65. End Property
    66. Public Sub addPosition(position As Position)
    67. _bill._positions.Add(position)
    68. OnPropertyChanged("PositionList")
    69. End Sub
    70. End Class
    71. Public MustInherit Class BaseViewModell
    72. Implements INotifyPropertyChanged
    73. Public Event PropertyChanged As PropertyChangedEventHandler
    74. Protected Sub OnPropertyChanged(propertyName As String)
    75. RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
    76. End Sub
    77. Protected Overridable Sub OnPropertyChanged(e As PropertyChangedEventArgs)
    78. RaiseEvent PropertyChanged(Me, e)
    79. End Sub
    80. Protected Sub RaisePropertyChanged(propertyName As String)
    81. Dim e As New PropertyChangedEventArgs(propertyName)
    82. OnPropertyChanged(e)
    83. End Sub
    84. End Class
    85. End Namespace


    Mein XAML-Code:

    XML-Quellcode

    1. <Grid>
    2. <DataGrid x:Name="dataGrid" ItemsSource="{Binding PositionList, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" Margin="25,46,0,0" VerticalAlignment="Top" Height="152" Width="390" CanUserAddRows="True" RowEditEnding="dataGrid_RowEditEnding"/>
    3. </Grid>


    Gruß,

    Mario
    Hallo nochmal,

    hab es gelöst:

    XML-Quellcode

    1. <DataGrid.Columns>
    2. <DataGridTextColumn Binding="{Binding PositionId, NotifyOnTargetUpdated=True,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="100" Header="PositionId" IsReadOnly="True" />
    3. <DataGridTextColumn Binding="{Binding Cash, NotifyOnTargetUpdated=True, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="100" Header="Cash"/>
    4. </DataGrid.Columns>


    Man muss für jede Spalte "UpdateSourceTrigger=PropertyChanged" setzen.

    Gruß,

    Mario