Ejemplo n.º 1
0
 /// <summary>
 ///     Cancel pending edits and then execute a refresh on the collection view to prevent any exceptions thrown.
 /// </summary>
 /// <param name="collectionView">The list collection view that should be refreshed.</param>
 public static void SafeRefresh(this ListCollectionView collectionView)
 {
     if (collectionView.IsEditingItem)
     {
         collectionView.CancelEdit();
     }
     collectionView.Refresh();
 }
Ejemplo n.º 2
0
        // There is a weird bug in DataGrid that blows if there is a sort in place when items are changed.
        //
        //System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.InvalidOperationException: 'Sorting' is not allowed during an AddNew or EditItem transaction.
        //at System.Windows.Data.ListCollectionView.SortDescriptionsChanged(Object sender, NotifyCollectionChangedEventArgs e)
        //at System.Collections.Specialized.NotifyCollectionChangedEventHandler.Invoke(Object sender, NotifyCollectionChangedEventArgs e)
        //at System.Windows.Controls.ItemCollection.CloneList(IList clone, IList master)
        //at System.Windows.Controls.ItemCollection.SynchronizeSortDescriptions(NotifyCollectionChangedEventArgs e, SortDescriptionCollection origin, SortDescriptionCollection clone)
        //at System.Windows.Controls.ItemCollection.SortDescriptionsChanged(Object sender, NotifyCollectionChangedEventArgs e)
        //at System.Collections.Specialized.NotifyCollectionChangedEventHandler.Invoke(Object sender, NotifyCollectionChangedEventArgs e)
        //at System.Windows.Controls.DataGrid.ClearSortDescriptionsOnItemsSourceChange()
        //at System.Windows.Controls.DataGrid.OnCoerceItemsSourceProperty(DependencyObject d, Object baseValue)
        //at System.Windows.DependencyObject.ProcessCoerceValue(DependencyProperty dp, PropertyMetadata metadata, EntryIndex& entryIndex, Int32& targetIndex, EffectiveValueEntry& newEntry, EffectiveValueEntry& oldEntry, Object& oldValue, Object baseValue, Object controlValue, CoerceValueCallback coerceValueCallback, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, Boolean skipBaseValueChecks)
        //at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
        //at System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp, Object value, PropertyMetadata metadata, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType, Boolean isInternal)
        //at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value)
        //at Walkabout.Views.Controls.TransactionsView.Display(IList data, TransactionViewName name, String caption, Int64 selectedRowId)
        public void SetItemsSource(IEnumerable items)
        {
            ListCollectionView view = CollectionViewSource.GetDefaultView(this.ItemsSource) as ListCollectionView;

            if (view != null)
            {
                if (view.IsAddingNew || view.IsEditingItem)
                {
                    if (view.IsAddingNew)
                    {
                        try
                        {
                            view.CancelNew();
                        }
                        catch
                        {
                        }
                    }
                    else if (view.IsEditingItem)
                    {
                        try
                        {
                            view.CancelEdit();
                        }
                        catch
                        {
                            // sometimes this throws but also removes IsEditing...
                        }
                    }
                }
            }

            var sorted = RemoveSort();

            if (sorted != null)
            {
                this.sorted = sorted;
            }
            try
            {
                // sometimes we get a weird exception saying
                // 'DeferRefresh' is not allowed during an AddNew or EditItem transaction
                this.ItemsSource = items;
            }
            catch (Exception ex)
            {
                // I want to see these errors
                if (MessageBoxEx.Show(ex.ToString() + "\n\nDo you want to try again?", "Debug Error", MessageBoxButton.OKCancel, MessageBoxImage.Error) == MessageBoxResult.OK)
                {
                    // and try again later...at low priority so that the UI has a chance to settle down...
                    Dispatcher.BeginInvoke(new Action(() =>
                    {
                        SetItemsSource(items);
                    }), DispatcherPriority.Background);
                }
            }
        }