views:

231

answers:

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

I wish there would be an even like PreviewInitializingNewItem or NewItemInitialized, or alternatively unlocking the Items property so I can refresh the view.

Any workaround? The only way I could do is making a button 'Add row' and manually adding the new instance to the collection, but I WOULD prefer to use the DataGrid new row advantage.