views:

61

answers:

0

Hi,

I have a datagrid with editable items in it and I have a button to create a new instance of such an item. The datagrid is updated with the new item but I can't select the recently added item and start editing on it.

Below is my code:

  private void btnNewKenmerk_Click( object sender, RoutedEventArgs e )
  {
   Kenmerk newKenmerk = new Kenmerk(); // the item to add

   Kenmerken.Add( newKenmerk ); // this is an observablecollection. the datagrid (dgKenmerken) has this as itemssource

   // deselect all other items except our new kenmerk
   for( int i = 0; i < dgKenmerken.Items.Count; i++ )
   {
    Kenmerk kenmerk = ( Kenmerk )dgKenmerken.Items[ i ];
    DataGridRow dgRow = ( DataGridRow )dgKenmerken.ItemContainerGenerator.ContainerFromIndex( i );

    if( dgRow != null )
    {
     dgRow.IsSelected = ( kenmerk == newKenmerk );
    }
   }

   dgKenmerken.SelectedItem = newKenmerk;

   // start editing
   if( DataGrid.BeginEditCommand.CanExecute( newKenmerk, dgKenmerken ) )
   {
    DataGrid.BeginEditCommand.Execute( newKenmerk, dgKenmerken );
   }
  }

The item is added and the background of the row is changed, but the BeginEditCommand starts editing on my previous selected item, not the added item. Anyone has any clue how to fix this?