7

オブジェクトのObservableCollectionにバインドするDataGridがWPFアプリケーションにあり、すべてが正常に機能します。ここで、実行時にデータグリッドのセルを変更し、コンテンツを削除する場合は、セルを空のままにします。observableCollectionの対応する値は変更されず、古い値になります。しかし、データグリッドを含むウィンドウを終了してウィンドウを再起動すると、XamlParseExceptionがスローされ、「SetProperty'System.Windows.Controls.ItemsControl.ItemsSource'が例外をスローしました」と表示されます。

  StackTrace:
       at System.Windows.Markup.XamlReader.RewrapException(Exception e, IXamlLineInfo lineInfo, Uri baseUri)
       at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
       at System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader, Boolean skipJournaledProperties, Object rootObject, XamlAccessLevel accessLevel, Uri baseUri)
       at System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream)
       at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
       at VO3.Start.InitializeComponent() in c:\VO\Trunk\VO3\Start.xaml:line 1
       at VO3.Start..ctor() in C:\VO\Trunk\VO3\Start.xaml.cs:line 103
  InnerException: System.InvalidOperationException
       Message='DeferRefresh' is not allowed during an AddNew or EditItem transaction.
       Source=PresentationFramework
       StackTrace:
            at System.Windows.Data.CollectionView.DeferRefresh()
            at System.Windows.Controls.ItemCollection.SetCollectionView(CollectionView view)
            at System.Windows.Controls.ItemCollection.SetItemsSource(IEnumerable value)
            at System.Windows.Controls.ItemsControl.OnItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
            at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
            at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
            at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
            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 System.Windows.Baml2006.WpfKnownMemberInvoker.SetValue(Object instance, Object value)
            at MS.Internal.Xaml.Runtime.ClrObjectRuntime.SetValue(XamlMember member, Object obj, Object value)
            at MS.Internal.Xaml.Runtime.ClrObjectRuntime.SetValue(Object inst, XamlMember property, Object value)
       InnerException: 

ウィンドウを閉じたときにデータグリッド内のセルが空でない限り、例外はスローされません。また、ウィンドウがInitializeComponent Lineを実行する前にコレクションをチェックしましたが、完全に正常に見えます。すべてのオブジェクトに正しい値があり、空やnullはありません。なぜこの例外がスローされるのか理解できません。何か案は?前もって感謝します。

XAML:

<DataGrid HeadersVisibility="All" RowHeight="19" AutoGenerateColumns="False" Grid.Row="1" CanUserResizeColumns="False"
                              CanUserAddRows="False" CanUserDeleteRows="False" Block.TextAlignment="Right" Grid.RowSpan="2" CanUserReorderColumns="False"
                              ItemsSource="{Binding Collection}" Width="132" HorizontalAlignment="Right" Margin="10,0,10,0" CanUserSortColumns="False"
                              IsEnabled="{Binding ElementName=CheckBox, Path=SelectedIndex, Converter={StaticResource startEnabledConverter}}">
4

2 に答える 2

4

DataGridとDeferRefreshにはかなりの数の問題があったようです。ここに2つの関連する 投稿があります:

一般的な推奨事項は、DataGridに関連付けられたビューをIEditableCollectionViewとして調べ(最後に調べたときにObservableCollectionがバインド中にListCollectionViewを取得するため、デフォルトで表示されます)、IsAddingNewとIsEditingItemをチェックしてビューを安全に変更できるかどうかを確認することです。このような変更には、ビューへの変更をまとめるために使用される本質的に使い捨てであるDeferRefreshが含まれます。

私が見る限り、問題は、DataGridがトランザクションの途中にあるか、ItemsSourceを変更しているときにビューがそれを信じるように設定されていることです(これにより、関連付けられたバインドされたビューが変更または完全に置き換えられます)。セルをクリアしているが、基になるデータがまだ変更されていないということは、編集が開始されている(BeginEdit)が、まだコミットされていない(CommitEdit)ことを意味します。これは、デフォルトでセルを変更したときに発生します。

ウィンドウを閉じて再度開いているときに発生しているということは、トランザクションが進行中であると考えている保存状態が維持されていることを意味します。おそらく、以前と同じListCollectionViewを使用していて、IsEditingItemがtrueに設定されているのでしょうか。

この修正により、ウィンドウを閉じる前にすべての編集が確実にコミットされるようになる可能性があります。

于 2011-10-28T15:14:11.700 に答える
0

空白にしているフィールドが文字列型でない場合は、正しく更新されていない可能性があります。

たとえば、それが「int」であり、セルを空白にした場合、ビューモデルの「int」プロパティは設定されません。

この問題を修正する方法は、intの空白値をゼロに変換するコンバーターを使用することです。

お役に立てれば

于 2011-08-15T01:52:38.670 に答える