WPF DataGrid duplicates new row when new item is attached to the source collection.

Posted by Shimmy on Stack Overflow See other posts from Stack Overflow or by Shimmy
Published on 2010-04-27T23:39:30Z Indexed on 2010/04/27 23:43 UTC
Read the original article Hit count: 678

<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

© Stack Overflow or respective owner

Related posts about wpf

Related posts about xaml