簡體   English   中英

WPF DataGrid ItemsSource綁定問題

[英]WPF DataGrid ItemsSource Binding Issue

我在WPF應用程序中有一個DataGrid,它將自己綁定到一個ObservableCollection對象,一切正常。 現在,如果我在運行時修改數據網格中的單元格並刪除內容,請將單元格留空。 observableCollection的對應值不會被修改,它將是舊值。 但是當我退出包含datagrid的窗口並重新啟動窗口時,它會拋出一個XamlParseException,說:“設置屬性'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: 

只要關閉窗口時,它就不會拋出異常,數據網格中的任何單元格都不為空。 我還在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}}">

DataGrid和DeferRefresh似乎存在很多問題。 這是兩個相關的 帖子

通用建議似乎是將DataGrid的關聯視圖檢查為IEditableCollectionView(默認情況下是你的,因為ObservableCollection在最后一次查看時在綁定期間獲取了ListCollectionView)並檢查IsAddingNew和IsEditingItem以查看修改視圖是否安全。 這些修改將包括DeferRefresh,它本質上是一個用於批量修改視圖的Disposable。

據我所知,您遇到的問題是DataGrid位於事務的中間,或者視圖設置為相信它,而您正在更改ItemsSource(這將更改或完全替換關聯的綁定視圖)。 您正在清除單元格但尚未修改基礎數據意味着它已開始編輯(BeginEdit),但尚未提交(CommitEdit),這在默認情況下更改單元格時會發生。

當你關閉並重新打開窗口時發生這種情況意味着保留了一些保留狀態仍然認為事務正在進行中。 也許它仍然使用與以前相同的ListCollectionView,並且仍然將IsEditingItem設置為true?

該修復最有可能確保在關閉窗口之前提交任何和所有編輯。

如果您正在消隱的字段不是字符串類型,則可能無法正確更新。

例如,如果它是一個“int”並且您將單元格空白,則視圖模型的“int”屬性將不會被設置。

解決這個問題的方法是使用轉換器將int的空白值轉換為零。

希望這可以幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM