我有一個WPF應用程序中的DataGrid,它自己綁定到一個ObservableCollection對象,並且一切正常。 現在,如果我在運行時修改數據網格中的單元格並刪除內容,請將單元格留空。 observableCollection的相應值不會被修改,它會是舊值。但是,當我退出包含DataGrid中的窗口,並重新啓動窗口,它拋出一個XamlParseException,說:「設置屬性‘System.Windows.Controls.ItemsControl.ItemsSource’拋出一個異常」WPF DataGrid 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:
它不會拋出異常,只要當我關閉窗口時,datagrid中的單元格就不會爲空。我還在窗口初始化組件行前檢查了集合,它看起來非常好,所有對象都有正確的值,沒有空或空值。我無法弄清楚它爲什麼拋出這個異常。有任何想法嗎? 在此先感謝。
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等)。 – CodeWarrior 2011-03-08 00:15:22
集合中對象的屬性不可爲空,它們在propertychanged上更新。但從過去的經驗來看,當字段留空並不意味着集合中的相應對象爲空。並且在值被留空後我檢查了集合中的對象仍然是舊值。在窗口再次打開之前,我再次檢查了這些值,並且沒有空的屬性。出於某種原因,當WPF在DataGrid的ItemsSource DependencyProperty的set屬性中引發異常時 – 2011-03-08 13:40:56
我建議您查找綁定到相同ObservableCollection的任何其他UI部分,並刪除對該集合的所有綁定以便測試你以上問題再次孤立。如果幸運的話,這只是其他控件使用的相應CollectionView的一個問題。 – 2011-11-29 10:26:46