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:
Mein XAML-Code:
Gruß,
Mario
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
- Imports System.Windows
- Imports System.Diagnostics
- Namespace MVVMProject
- Public Partial Class MainWindow
- Inherits Window
- Private vm As New BillViewModel()
- Public Sub New()
- InitializeComponent()
- vm.addPosition(New Position(1, 111, 1.5, 10.45))
- vm.addPosition(New Position(2, 222, 2.5, 20.45))
- vm.addPosition(New Position(3, 333, 3.5, 30.45))
- DataContext = vm
- End Sub
- 'Liefert nach Bearbeitung im View nicht die neuen Daten
- Private Sub dataGrid_RowEditEnding(sender As Object, e As System.Windows.Controls.DataGridRowEditEndingEventArgs)
- For Each item As Position In vm.PositionList
- Debug.WriteLine(item.ToString())
- Next
- End Sub
- End Class
- Public Class BillViewModel
- Inherits BaseViewModell
- Private _bill As Bill
- Public Sub New()
- _bill = New Bill()
- End Sub
- Public Sub New(_bill As Bill)
- Me.Bill = _bill
- End Sub
- Public Property Bill() As Bill
- Get
- Return _bill
- End Get
- Set
- _bill = value
- OnPropertyChanged("Bill")
- End Set
- End Property
- Public Property BillId() As Integer
- Get
- Return _bill._billId
- End Get
- Set
- _bill._billId = value
- OnPropertyChanged("BillId")
- End Set
- End Property
- Public Property [Date]() As DateTime
- Get
- Return Bill._date
- End Get
- Set
- Bill._date = value
- OnPropertyChanged("Date")
- End Set
- End Property
- Public Property PositionList() As ObservableCollection(Of Position)
- Get
- Return Bill._positions
- End Get
- Set
- Bill._positions = value
- OnPropertyChanged("PositionList")
- End Set
- End Property
- Public Sub addPosition(position As Position)
- _bill._positions.Add(position)
- OnPropertyChanged("PositionList")
- End Sub
- End Class
- Public MustInherit Class BaseViewModell
- Implements INotifyPropertyChanged
- Public Event PropertyChanged As PropertyChangedEventHandler
- Protected Sub OnPropertyChanged(propertyName As String)
- RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
- End Sub
- Protected Overridable Sub OnPropertyChanged(e As PropertyChangedEventArgs)
- RaiseEvent PropertyChanged(Me, e)
- End Sub
- Protected Sub RaisePropertyChanged(propertyName As String)
- Dim e As New PropertyChangedEventArgs(propertyName)
- OnPropertyChanged(e)
- End Sub
- End Class
- End Namespace
Mein XAML-Code:
XML-Quellcode
Gruß,
Mario