WPF DataGrid duplicates new row when new item is attached to the source collection.
- by Shimmy
<Page>
<Page.Resources>
<data:Quote x:Key="Quote"/>
</Page.Resources>
<tk:DataGrid
DataContext="{Binding Quote}"
ItemsSource="{Binding Rooms}">
<tk:DataGrid/>
</Page>
Code:
Private Sub InitializingNewItem _
(sender As DataGrid, _
ByVal e As InitializingNewItemEventArgs) _
Handles dgRooms.InitializingNewItem
Dim room = DirectCast(e.NewItem, Room) 'Room is subclass of EntityObject
Dim state = room.EntityState 'Detached
Dim quote = Resources("Quote")
state = quote.EntityState 'Unchanged
'either one of these lines causes the new row to go duplicated:
quote.Rooms.Add(room)
room.Quote = quote
'I tried:
sender.Items.Refresh
'I also tried to remove the detached entity from the DataGrid and create a
'new item but it they throw exceptions saying the the Items is untouchable.
End If