Wpf 错误“;指定的元素已经是另一个元素的逻辑子元素”;?-根本原因

Wpf 错误“;指定的元素已经是另一个元素的逻辑子元素”;?-根本原因,wpf,xaml,Wpf,Xaml,我不知道这是从哪里来的。以下是调试模式下的完整堆栈跟踪。如何找到根本原因 下面的堆栈跟踪并没有详细说明错误的来源,内部异常为null at System.Windows.FrameworkElement.ChangeLogicalParent(DependencyObject newParent) at System.Windows.FrameworkElement.AddLogicalChild(Object child) at System.Windows.Controls.Co

我不知道这是从哪里来的。以下是调试模式下的完整堆栈跟踪。如何找到根本原因

下面的堆栈跟踪并没有详细说明错误的来源,内部异常为null

at System.Windows.FrameworkElement.ChangeLogicalParent(DependencyObject newParent)
   at System.Windows.FrameworkElement.AddLogicalChild(Object child)
   at System.Windows.Controls.ContentControl.OnContentChanged(Object oldContent, Object newContent)
   at System.Windows.Controls.ContentControl.OnContentChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
   at System.Windows.PropertyChangedCallback.Invoke(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.CoerceValue(DependencyProperty dp)
   at System.Windows.Controls.DataGridHelper.TransferProperty(DependencyObject d, DependencyProperty p)
   at System.Windows.Controls.Primitives.DataGridColumnHeader.PrepareColumnHeader(Object item, DataGridColumn column)
   at System.Windows.Controls.Primitives.DataGridColumnHeadersPresenter.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.DataGridCellsPanel.InsertContainer(Int32 childIndex, UIElement container, Boolean isRecycled)
   at System.Windows.Controls.DataGridCellsPanel.AddContainerFromGenerator(Int32 childIndex, UIElement child, Boolean newlyRealized)
   at System.Windows.Controls.DataGridCellsPanel.GenerateChild(IItemContainerGenerator generator, Size constraint, DataGridColumn column, Int32& childIndex, Size& childSize)
   at System.Windows.Controls.DataGridCellsPanel.DetermineRealizedColumnsBlockList(Size constraint)
   at System.Windows.Controls.DataGridCellsPanel.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.Media.MediaContext.Resize(ICompositionTarget resizedCompositionTarget)
   at System.Windows.Interop.HwndTarget.OnResize()
   at System.Windows.Interop.HwndTarget.HandleMessage(WindowMessage msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Interop.HwndSource.HwndTargetFilterMessage(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)
更新1:问题已通过使用修复

HeaderTeamplate = {StaticResource BALimitedHeaderTemplate}
而不是

<DataGridTemplateColumn.Header>
  <ContentPresenter ContentTemplate="{StaticResource BALimitedHeaderTemplate}"   x:Name="_BAContentPresenter"/>
</DataGridTemplateColumn.Header>

上面带有ContentPresenter的行是多次动态添加到父窗口的用户控件的一部分。我仍然不确定,为什么将用户控件(单独的实例)添加到窗口会导致异常——更奇怪的是,只有在.Net framework 4.0中,而不是在4.5及更高版本中。

更新2:显然这是FrameworkV4.0中的一个bug,使用起来似乎相当合法。这个问题在框架4.5及更高版本中得到了解决!我们使用HeaderTemplate解决了这个问题。其他人试图在下面的链接中描述这个问题


您正在手动尝试将可视项添加到另一个窗口或控件集合中,该窗口或控件集合已分配有窗口或控件集合。此异常仅出现在生产中的几台机器上-rest所有机器都工作正常。我刚刚将framework更新为4.5(从原来的4.0)在这个问题出现的机器上,在框架更新之后,它就消失了。这很好。这仍然是由我说的话引起的。现在,由于竞争条件的复杂性增加,我不理解竞争条件部分。框架4.5更新后的情况如何?您正在手动尝试将可视项添加到另一个窗口或控件集合,而该窗口或控件集合已经分配了一个窗口或控件集合。此异常仅出现在生产中的几台机器上-其余所有机器都工作正常。我刚刚将框架更新为4.5(从最初的4.0版开始)在这个问题出现的机器上,在框架更新之后它就消失了。这很好。它仍然是由我所说的引起的。现在随着竞争条件的增加,我不理解竞争条件部分。在框架4.5更新中,这是怎么出现的?