【发布时间】:2011-07-10 14:06:38
【问题描述】:
我在 WPF 应用程序中有一个 DataGrid,它将自身绑定到对象的 ObservableCollection,并且一切正常。 现在,如果我在运行时修改数据网格中的单元格并删除内容,请将单元格留空。 observableCollection 对应的值不会被修改,会是旧值。但是当我退出包含数据网格的窗口并重新启动窗口时,它会抛出 XamlParseException,说:“Set Property 'System.Windows.Controls.ItemsControl.ItemsSource' throw an exception”
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:
只要我关闭窗口时,数据网格中没有单元格为空,它就不会抛出异常。我还在 Window 执行 InitializeComponent Line 之前检查了 Collection,它看起来非常好,所有对象都有正确的值,没有空值或空值。我无法弄清楚为什么它会抛出这个异常。有任何想法吗? 提前致谢。
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}}">
【问题讨论】:
-
集合中项目的属性是否可以为空?您的属性何时更新(例如 PropertyChanged、LoseFocus 等)。
-
集合中对象的属性不可为空,它们在 propertychanged 时更新。但从过去的经验来看,当该字段留空时,并不意味着集合中的相应对象为空。并且我在值留空后检查了集合中的对象仍然是旧值。在窗口再次打开之前,我再次检查了值并且没有空属性。由于某种原因,WPF 在到达 DataGrid 的 ItemsSource DependencyProperty 的 set 属性时引发异常
-
我建议您寻找绑定到同一个 ObservableCollection 的 UI 的任何其他部分,并删除与该集合的所有绑定,以便再次单独测试上述问题。如果幸运的话,这只是对应的 CollectionView 被其他控件使用的问题。
标签: wpf exception datagrid binding itemssource