Esempio n. 1
0
    protected virtual void OnDeleteExecuted( object sender, ExecutedRoutedEventArgs e )
    {
      CancelRoutedEventArgs args = new CancelRoutedEventArgs( DataGridControl.DeletingSelectedItemsEvent, this );
      this.OnDeletingSelectedItems( args );

      if( args.Cancel )
        return;

      // Copy the selected item in a temp hashtable.
      List<KeyValuePair<DataGridContext, object[]>> selectedItemList = new List<KeyValuePair<DataGridContext, object[]>>();

      for( int i = m_selectedContexts.Count - 1; i >= 0; i-- )
      {
        DataGridContext selectedContext = m_selectedContexts[ i ];
        IList<object> sourceSelectedItems = selectedContext.SelectedItems;
        object[] clonedSelectedItems = new object[ sourceSelectedItems.Count ];
        sourceSelectedItems.CopyTo( clonedSelectedItems, 0 );
        selectedItemList.Add( new KeyValuePair<DataGridContext, object[]>( selectedContext, clonedSelectedItems ) );
      }

      bool raiseItemDeleted = false;

      // Delete the selected item
      foreach( KeyValuePair<DataGridContext, object[]> keyValuePair in selectedItemList )
      {
        DataGridContext dataGridContext = keyValuePair.Key;
        object[] selectedItems = keyValuePair.Value;

        if( ( !dataGridContext.IsDeleteCommandEnabled ) || ( selectedItems.Length == 0 ) )
          continue;

        DataGridCollectionViewBase dataGridCollectionViewBase =
          dataGridContext.ItemsSourceCollection as DataGridCollectionViewBase;

        IList list = ( dataGridCollectionViewBase == null )
          ? ItemsSourceHelper.TryGetIList( dataGridContext.ItemsSourceCollection )
          : null; // If we already have a DataGridCollectionView, no need to look for an IList source.

        if( ( dataGridCollectionViewBase != null )
          || ( ( list != null ) && DataGridControl.IsRemoveAllowedForDeleteCommand( list ) ) )
        {
          foreach( object item in selectedItems )
          {
            bool retry = false;

            do
            {
              try
              {
                int itemIndex;

                if( dataGridCollectionViewBase != null )
                {
                  itemIndex = dataGridCollectionViewBase.IndexOf( item );

                  if( itemIndex != -1 )
                    dataGridCollectionViewBase.RemoveAt( itemIndex );
                }
                else if( list != null )
                {
                  itemIndex = list.IndexOf( item );

                  if( itemIndex != -1 )
                    list.RemoveAt( itemIndex );
                }

                retry = false;
                raiseItemDeleted = true;
              }
              catch( Exception ex )
              {
                DeletingSelectedItemErrorRoutedEventArgs deletingItemErrorArgs = new DeletingSelectedItemErrorRoutedEventArgs( item, ex, DataGridControl.DeletingSelectedItemErrorEvent, this );
                this.OnDeletingSelectedItemError( deletingItemErrorArgs );

                switch( deletingItemErrorArgs.Action )
                {
                  case DeletingSelectedItemErrorAction.Abort:
                    return;

                  default:
                    retry = ( deletingItemErrorArgs.Action == DeletingSelectedItemErrorAction.Retry );
                    break;
                }
              }
            }
            while( retry );
          }
        }
      }

      if( raiseItemDeleted )
        this.OnSelectedItemsDeleted();
    }
Esempio n. 2
0
 protected virtual void OnDeletingSelectedItemError( DeletingSelectedItemErrorRoutedEventArgs e )
 {
   this.RaiseEvent( e );
 }