2012-12-13 66 views
4

當試圖在DataGrid上向下滾動時,我得到一個InvalidOperationException異常,指出「綁定表達式已經屬於BindingGroup;它不能被添加到不同的BindingGroup」。我不知道任何BindingGroups在使用中,所以我不確定它在說什麼。我已經縮小到只發生在下面的列中。我懷疑它也與虛擬化有關。有任何想法嗎?當滾動DataGrid時出現InvalidOperationException:綁定表達式已經屬於BindingGroup

這裏是DataGrid的標記:

<DataGrid x:Name="dgStudentSNP" 
       AutoGenerateColumns="False" 

       Grid.Row="1" Margin="6,10" 
       HorizontalAlignment="Stretch" 
       HeadersVisibility="Column" 
       CanUserSortColumns="True" 
       CanUserAddRows="False" 
       CanUserResizeColumns="True" 
       CanUserDeleteRows="False" 
       BorderThickness="1" 
       SelectedValuePath="SNPEnrollmentID" VerticalScrollBarVisibility="Auto" VerticalAlignment="Top" Height="Auto" IsReadOnly="True"> 
      <DataGrid.Columns> 
       <DataGridTemplateColumn Header="Student Name" MinWidth="120"> 
        <DataGridTemplateColumn.CellTemplate> 
         <DataTemplate> 
          <TextBlock> 
            <Hyperlink Click="GetStudentFamily" > 
             <Run> 
              <Run.Text > 
               <MultiBinding StringFormat="{}{0} {1}"> 
                <Binding Path="FirstName"/> 
                <Binding Path="LastName" /> 
               </MultiBinding> 
              </Run.Text>                
             </Run> 
            </Hyperlink> 
          </TextBlock> 
         </DataTemplate> 
        </DataGridTemplateColumn.CellTemplate> 
       </DataGridTemplateColumn> 
       </DataGrid.Columns> 
      </DataGrid> 

這裏是異常的堆棧跟蹤:

Stack Trace : at System.Windows.Data.BindingExpressionBase.JoinBindingGroup(BindingGroup bg, Boolean explicitJoin) 
    at System.Windows.Data.BindingExpressionBase.RejoinBindingGroup(Boolean isReflective, DependencyObject contextElement) 
    at System.Windows.Data.BindingExpression.OnDataContextChanged(DependencyObject contextElement) 
    at System.Windows.Data.BindingExpression.HandlePropertyInvalidation(DependencyObject d, DependencyPropertyChangedEventArgs args) 
    at System.Windows.Data.BindingExpressionBase.OnPropertyInvalidation(DependencyObject d, DependencyPropertyChangedEventArgs args) 
    at System.Windows.Data.BindingExpression.OnPropertyInvalidation(DependencyObject d, DependencyPropertyChangedEventArgs args) 
    at System.Windows.Data.MultiBindingExpression.HandlePropertyInvalidation(DependencyObject d, DependencyPropertyChangedEventArgs args) 
    at System.Windows.Data.BindingExpressionBase.OnPropertyInvalidation(DependencyObject d, DependencyPropertyChangedEventArgs args) 
    at System.Windows.DependentList.InvalidateDependents(DependencyObject source, DependencyPropertyChangedEventArgs sourceArgs) 
    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.TreeWalkHelper.OnInheritablePropertyChanged(DependencyObject d, InheritablePropertyChangeInfo info) 
    at System.Windows.DescendentsWalker`1._VisitNode(DependencyObject d) 
    at System.Windows.DescendentsWalker`1.VisitNode(DependencyObject d) 
    at System.Windows.DescendentsWalker`1.WalkLogicalChildren(FrameworkElement feParent, FrameworkContentElement fceParent, IEnumerator logicalChildren) 
    at System.Windows.DescendentsWalker`1.IterateChildren(DependencyObject d) 
    at System.Windows.DescendentsWalker`1._VisitNode(DependencyObject d) 
    at System.Windows.DescendentsWalker`1.VisitNode(DependencyObject d) 
    at System.Windows.DescendentsWalker`1.WalkLogicalChildren(FrameworkElement feParent, FrameworkContentElement fceParent, IEnumerator logicalChildren) 
    at System.Windows.DescendentsWalker`1.WalkFrameworkElementLogicalThenVisualChildren(FrameworkElement feParent, Boolean hasLogicalChildren) 
    at System.Windows.DescendentsWalker`1.IterateChildren(DependencyObject d) 
    at System.Windows.DescendentsWalker`1._VisitNode(DependencyObject d) 
    at System.Windows.DescendentsWalker`1.VisitNode(FrameworkElement fe) 
    at System.Windows.DescendentsWalker`1.VisitNode(DependencyObject d) 
    at System.Windows.DescendentsWalker`1.WalkFrameworkElementLogicalThenVisualChildren(FrameworkElement feParent, Boolean hasLogicalChildren) 
    at System.Windows.DescendentsWalker`1.IterateChildren(DependencyObject d) 
    at System.Windows.DescendentsWalker`1.StartWalk(DependencyObject startNode, Boolean skipStartNode) 
    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.Controls.ContentPresenter.OnContentChanged(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.InvalidateProperty(DependencyProperty dp) 
    at System.Windows.Data.BindingExpressionBase.Invalidate(Boolean isASubPropertyChange) 
    at System.Windows.Data.BindingExpression.TransferValue(Object newValue, Boolean isASubPropertyChange) 
    at System.Windows.Data.BindingExpression.Activate(Object item) 
    at System.Windows.Data.BindingExpression.OnDataContextChanged(DependencyObject contextElement) 
    at System.Windows.Data.BindingExpression.HandlePropertyInvalidation(DependencyObject d, DependencyPropertyChangedEventArgs args) 
    at System.Windows.Data.BindingExpressionBase.OnPropertyInvalidation(DependencyObject d, DependencyPropertyChangedEventArgs args) 
    at System.Windows.Data.BindingExpression.OnPropertyInvalidation(DependencyObject d, DependencyPropertyChangedEventArgs args) 
    at System.Windows.DependentList.InvalidateDependents(DependencyObject source, DependencyPropertyChangedEventArgs sourceArgs) 
    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.Controls.ItemContainerGenerator.LinkContainerToItem(DependencyObject container, Object item, Boolean isRecycled) 
    at System.Windows.Controls.ItemContainerGenerator.OnItemReplaced(Object oldItem, Object newItem, Int32 index) 
    at System.Windows.Controls.ItemContainerGenerator.OnCollectionChanged(Object sender, NotifyCollectionChangedEventArgs args) 
    at System.Windows.Controls.ItemContainerGenerator.System.Windows.IWeakEventListener.ReceiveWeakEvent(Type managerType, Object sender, EventArgs e) 
    at System.Windows.WeakEventManager.DeliverEventToList(Object sender, EventArgs args, ListenerList list) 
    at System.Windows.WeakEventManager.DeliverEvent(Object sender, EventArgs args) 
    at System.Collections.Specialized.CollectionChangedEventManager.OnCollectionChanged(Object sender, NotifyCollectionChangedEventArgs args) 
    at System.Collections.Specialized.NotifyCollectionChangedEventHandler.Invoke(Object sender, NotifyCollectionChangedEventArgs e) 
    at System.Windows.Data.CollectionView.OnCollectionChanged(NotifyCollectionChangedEventArgs args) 
    at System.Windows.Controls.ItemCollection.System.Windows.IWeakEventListener.ReceiveWeakEvent(Type managerType, Object sender, EventArgs e) 
    at System.Windows.WeakEventManager.DeliverEventToList(Object sender, EventArgs args, ListenerList list) 
    at System.Windows.WeakEventManager.DeliverEvent(Object sender, EventArgs args) 
    at System.Collections.Specialized.CollectionChangedEventManager.OnCollectionChanged(Object sender, NotifyCollectionChangedEventArgs args) 
    at System.Windows.Data.CollectionView.OnCollectionChanged(NotifyCollectionChangedEventArgs args) 
    at System.Windows.Data.ListCollectionView.ProcessCollectionChangedWithAdjustedIndex(NotifyCollectionChangedEventArgs args, Int32 adjustedOldIndex, Int32 adjustedNewIndex) 
    at System.Windows.Data.ListCollectionView.ProcessCollectionChanged(NotifyCollectionChangedEventArgs args) 
    at System.Windows.Data.CollectionView.OnCollectionChanged(Object sender, NotifyCollectionChangedEventArgs args) 
    at System.Windows.Controls.MultipleCopiesCollection.OnCollectionChanged(NotifyCollectionChangedEventArgs e) 
    at System.Windows.Controls.MultipleCopiesCollection.set_CopiedItem(Object value) 
    at System.Windows.Controls.Primitives.DataGridCellsPresenter.OnItemChanged(Object oldItem, Object newItem) 
    at System.Windows.Controls.DataGridRow.OnItemChanged(Object oldItem, Object newItem) 
    at System.Windows.Controls.DataGridRow.NotifyPropertyChanged(DependencyObject d, String propertyName, DependencyPropertyChangedEventArgs e, DataGridNotificationTarget target) 
    at System.Windows.Controls.DataGridRow.OnNotifyRowPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
    at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e) 
    at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e) 
    at System.Windows.Controls.DataGridRow.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.Controls.DataGridRow.PrepareRow(Object item, DataGrid owningDataGrid) 
    at System.Windows.Controls.DataGrid.PrepareContainerForItemOverride(DependencyObject element, Object item) 
    at System.Windows.Controls.ItemsControl.MS.Internal.Controls.IGeneratorHost.PrepareItemContainer(DependencyObject container, Object item) 
    at System.Windows.Controls.ItemContainerGenerator.System.Windows.Controls.Primitives.IItemContainerGenerator.PrepareItemContainer(DependencyObject container) 
    at System.Windows.Controls.VirtualizingStackPanel.InsertContainer(Int32 childIndex, UIElement container, Boolean isRecycled) 
    at System.Windows.Controls.VirtualizingStackPanel.AddContainerFromGenerator(Int32 childIndex, UIElement child, Boolean newlyRealized) 
    at System.Windows.Controls.VirtualizingStackPanel.MeasureOverride(Size constraint) 
    at System.Windows.Controls.Primitives.DataGridRowsPresenter.MeasureOverride(Size constraint) 
    at System.Windows.FrameworkElement.MeasureCore(Size availableSize) 
    at System.Windows.UIElement.Measure(Size availableSize) 
    at System.Windows.ContextLayoutManager.UpdateLayout() 
    at System.Windows.ContextLayoutManager.UpdateLayoutCallback(Object arg) 
    at System.Windows.Media.MediaContext.InvokeOnRenderCallback.DoWork() 
    at System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks() 
    at System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object resizedCompositionTarget) 
    at System.Windows.Media.MediaContext.RenderMessageHandler(Object resizedCompositionTarget) 
    at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs) 
    at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler) 
    at System.Windows.Threading.DispatcherOperation.InvokeImpl() 
    at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state) 
    at System.Threading.ExecutionContext.runTryCode(Object userData) 
    at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData) 
    at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state) 
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx) 
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 
    at System.Windows.Threading.DispatcherOperation.Invoke() 
    at System.Windows.Threading.Dispatcher.ProcessQueue() 
    at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) 
    at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) 
    at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o) 
    at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs) 
    at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler) 
    at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs) 
    at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam) 
    at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg) 
    at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame) 
    at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame) 
    at System.Windows.Application.RunDispatcher(Object ignore) 
    at System.Windows.Application.RunInternal(Window window) 
    at System.Windows.Application.Run(Window window) 
    at Oasis.App.Main() 
+0

你有沒有找到解決這個問題的辦法, 這也發生在我身上,我也是在MultiBinding裏面有一個單元格模板的情況。 –

+0

不,這聽起來像使用多個單獨的綁定或自定義屬性而不是MultiBindings可能是一個工作。 – xr280xr

+0

iv'e實際上讀了一些關於BindingGroups在我看來,Multibinding設置它自己的綁定組,它實際上應該從它的父節點繼承綁定組,我認爲這是一些可視化錯誤,我認爲設置BindingGroupName = null可能會修復此問題問題 http://msdn.microsoft.com/en-us/library/system.windows.data.bindinggroup。aspx 所以當對象變得被實現時,網格不會嘗試將它添加到它的BindingGroup中,但我不完全理解這是爲什麼,但這可能會解決這個問題。 –

回答

2

設置BindingGroupName爲null確實工作。這對我來說很神祕,我希望能理解發生了什麼變化的人會發表一個答案。但是,在文檔伊蘭otzap上面貼:

http://msdn.microsoft.com/en-us/library/system.windows.data.bindinggroup.aspx

它說以下內容:

您設置FrameworkElement的或FrameworkContentElement上的BindingGroup財產。子元素從其父元素繼承BindingGroup,就像使用其他可繼承屬性一樣。甲後代元件上結合加入到BindingGroup如果發生下列情況之一:

  • 結合的源,且具有的BindingGroup元件的DataContext的是相同的對象和BindingGroupName屬性未設置。

  • 綁定的BindingGroupName屬性等於BindingGroup的名稱,它們沒有顯式設置爲null。 在地址示例中,假設Panel的DataContext被設置爲Address類型的對象。每個TextBox的綁定都被添加到面板的BindingGroup中。

這樣看來,當你在DataGrid中使用multibinding的BindingGroup不匹配BindingGroupName的結合,所以當您嘗試更新這些綁定錯誤接踵而至。

的解決方案是明確設置MultiBinding的BindingGroupName爲null像

<MultiBinding StringFormat="{}{0} {1}" BindingGroupName="{x:Null}"> 
     <Binding Path="FirstName"/> 
     <Binding Path="LastName" /> 
    </MultiBinding> 

我不知道什麼樣的影響,這將以後的路線有,但它使得這個特定的異常消失。

相關問題