开发者

WPF datagrid start editing new item

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 );
 开发者_如何学Python   }
   }

   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?


It is because the datagrid does not "see" changes immediately. Postpone using your newly added data -- please try splitting your method into two -- one adding, the second using. Call the second from the first one, not directly, but via dispatcher.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜