2017-12-15 18 views
-1

私はWPF(.NET 4.5)クライアントアプリケーションに、仮想化の項目の巨大なリストを持っていますListBoxListBoxItemには、何らかのアクションを伴うContextMenuがあります。ListBox ContextMenuを仮想化するXamlParseException

ContextMenuを開くと、処理されない例外を受け取っているクライアントがあります。 Windows 10およびWindows Server 2012搭載の一部のPCで、ほぼ100%の再現率を達成しています。

System.Windows.Markup.XamlParseExceptionは、メッセージ:プロパティ 'System.Windows.Controls.HeaderedContentControl.Header'

設定が例外をスローしました。 ---> System.InvalidOperationException:指定された要素は既に別の要素の論理的な子です。最初に切断します。

注:私はx:Shared="False"を追加しようとしました - その問題のない修正

巨大な例外のStackTrace:(UserControl.Resourcesタグ内)

System.Windows.Markup.XamlParseException: Set property 'System.Windows.Controls.HeaderedContentControl.Header' threw an exception. ---> System.InvalidOperationException: Specified element is already the logical child of another element. Disconnect it first. 
at System.Windows.FrameworkElement.ChangeLogicalParent(DependencyObject newParent) 
at System.Windows.FrameworkElement.AddLogicalChild(Object child) 
at System.Windows.Controls.HeaderedItemsControl.OnHeaderChanged(Object oldHeader, Object newHeader) 
at System.Windows.Controls.HeaderedItemsControl.OnHeaderChanged(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) 
--- End of inner exception stack trace --- 
at System.Windows.FrameworkTemplate.LoadTemplateXaml(XamlReader templateReader, XamlObjectWriter currentWriter) 
at System.Windows.FrameworkTemplate.LoadTemplateXaml(XamlObjectWriter objectWriter) 
at System.Windows.FrameworkTemplate.LoadOptimizedTemplateContent(DependencyObject container, IComponentConnector componentConnector, IStyleConnector styleConnector, List'1 affectedChildren, UncommonField'1 templatedNonFeChildrenField) 
at System.Windows.FrameworkTemplate.LoadContent(DependencyObject container, List'1 affectedChildren) 
at System.Windows.StyleHelper.ApplyTemplateContent(UncommonField'1 dataField, DependencyObject container, FrameworkElementFactory templateRoot, Int32 lastChildIndex, HybridDictionary childIndexFromChildID, FrameworkTemplate frameworkTemplate) 
at System.Windows.FrameworkTemplate.ApplyTemplateContent(UncommonField'1 templateDataField, FrameworkElement container) 
at System.Windows.FrameworkElement.ApplyTemplate() 
at System.Windows.FrameworkElement.MeasureCore(Size availableSize) 
at System.Windows.UIElement.Measure(Size availableSize) 
at System.Windows.Controls.Control.MeasureOverride(Size constraint) 
at System.Windows.FrameworkElement.MeasureCore(Size availableSize) 
at System.Windows.UIElement.Measure(Size availableSize) 
at System.Windows.Controls.VirtualizingStackPanel.MeasureChild(IItemContainerGenerator& generator, IContainItemStorage& itemStorageProvider, IContainItemStorage& parentItemStorageProvider, Object& parentItem, Boolean& hasUniformOrAverageContainerSizeBeenSet, Double& computedUniformOrAverageContainerSize, Double& computedUniformOrAverageContainerPixelSize, Boolean& computedAreContainersUniformlySized, IList& items, Object& item, IList& children, Int32& childIndex, Boolean& visualOrderChanged, Boolean& isHorizontal, Size& childConstraint, Rect& viewport, VirtualizationCacheLength& cacheSize, VirtualizationCacheLengthUnit& cacheUnit, Boolean& foundFirstItemInViewport, Double& firstItemInViewportOffset, Size& stackPixelSize, Size& stackPixelSizeInViewport, Size& stackPixelSizeInCacheBeforeViewport, Size& stackPixelSizeInCacheAfterViewport, Size& stackLogicalSize, Size& stackLogicalSizeInViewport, Size& stackLogicalSizeInCacheBeforeViewport, Size& stackLogicalSizeInCacheAfterViewport, Boolean& mustDisableVirtualization, Boolean isBeforeFirstItem, Boolean isAfterFirstItem, Boolean isAfterLastItem, Boolean skipActualMeasure, Boolean skipGeneration, Boolean& hasBringIntoViewContainerBeenMeasured, Boolean& hasVirtualizingChildren) 
at System.Windows.Controls.VirtualizingStackPanel.MeasureOverrideImpl(Size constraint, Nullable'1& lastPageSafeOffset, List'1& previouslyMeasuredOffsets, Nullable'1& lastPagePixelSize, Boolean remeasure) 
at System.Windows.Controls.VirtualizingStackPanel.MeasureOverride(Size constraint) 
at System.Windows.FrameworkElement.MeasureCore(Size availableSize) 
at System.Windows.UIElement.Measure(Size availableSize) 
at MS.Internal.Helper.MeasureElementWithSingleChild(UIElement element, Size constraint) 
at System.Windows.Controls.ItemsPresenter.MeasureOverride(Size constraint) 
at System.Windows.FrameworkElement.MeasureCore(Size availableSize) 
at System.Windows.UIElement.Measure(Size availableSize) 
at MS.Internal.Helper.MeasureElementWithSingleChild(UIElement element, Size constraint) 
at System.Windows.Controls.ScrollContentPresenter.MeasureOverride(Size constraint) 
at System.Windows.FrameworkElement.MeasureCore(Size availableSize) 
at System.Windows.UIElement.Measure(Size availableSize) 
at System.Windows.Controls.Grid.MeasureOverride(Size constraint) 
at System.Windows.FrameworkElement.MeasureCore(Size availableSize) 
at System.Windows.UIElement.Measure(Size availableSize) 
at System.Windows.Controls.ScrollViewer.MeasureOverride(Size constraint) 
at System.Windows.FrameworkElement.MeasureCore(Size availableSize) 
at System.Windows.UIElement.Measure(Size availableSize) 
at System.Windows.Controls.Control.MeasureOverride(Size constraint) 
at System.Windows.FrameworkElement.MeasureCore(Size availableSize) 
at System.Windows.UIElement.Measure(Size availableSize) 
at System.Windows.Controls.Grid.MeasureCell(Int32 cell, Boolean forceInfinityV) 
at System.Windows.Controls.Grid.MeasureCellsGroup(Int32 cellsHead, Size referenceSize, Boolean ignoreDesiredSizeU, Boolean forceInfinityV, Boolean& hasDesiredSizeUChanged) 
at System.Windows.Controls.Grid.MeasureCellsGroup(Int32 cellsHead, Size referenceSize, Boolean ignoreDesiredSizeU, Boolean forceInfinityV) 
at System.Windows.Controls.Grid.MeasureOverride(Size constraint) 
at System.Windows.FrameworkElement.MeasureCore(Size availableSize) 
at System.Windows.UIElement.Measure(Size availableSize) 
at System.Windows.Controls.Decorator.MeasureOverride(Size constraint) 
at System.Windows.FrameworkElement.MeasureCore(Size availableSize) 
at System.Windows.UIElement.Measure(Size availableSize) 
at System.Windows.Controls.Grid.MeasureCell(Int32 cell, Boolean forceInfinityV) 
at System.Windows.Controls.Grid.MeasureCellsGroup(Int32 cellsHead, Size referenceSize, Boolean ignoreDesiredSizeU, Boolean forceInfinityV, Boolean& hasDesiredSizeUChanged) 
at System.Windows.Controls.Grid.MeasureCellsGroup(Int32 cellsHead, Size referenceSize, Boolean ignoreDesiredSizeU, Boolean forceInfinityV) 
at System.Windows.Controls.Grid.MeasureOverride(Size constraint) 
at System.Windows.FrameworkElement.MeasureCore(Size availableSize) 
at System.Windows.UIElement.Measure(Size availableSize) 
at System.Windows.Controls.Grid.MeasureCell(Int32 cell, Boolean forceInfinityV) 
at System.Windows.Controls.Grid.MeasureCellsGroup(Int32 cellsHead, Size referenceSize, Boolean ignoreDesiredSizeU, Boolean forceInfinityV, Boolean& hasDesiredSizeUChanged) 
at System.Windows.Controls.Grid.MeasureCellsGroup(Int32 cellsHead, Size referenceSize, Boolean ignoreDesiredSizeU, Boolean forceInfinityV) 
at System.Windows.Controls.Grid.MeasureOverride(Size constraint) 
at System.Windows.FrameworkElement.MeasureCore(Size availableSize) 
at System.Windows.UIElement.Measure(Size availableSize) 
at MS.Internal.Helper.MeasureElementWithSingleChild(UIElement element, Size constraint) 
at System.Windows.Controls.ContentPresenter.MeasureOverride(Size constraint) 
at System.Windows.FrameworkElement.MeasureCore(Size availableSize) 
at System.Windows.UIElement.Measure(Size availableSize) 
at System.Windows.Controls.Border.MeasureOverride(Size constraint) 
at System.Windows.FrameworkElement.MeasureCore(Size availableSize) 
at System.Windows.UIElement.Measure(Size availableSize) 
at System.Windows.Controls.Control.MeasureOverride(Size constraint) 
at System.Windows.FrameworkElement.MeasureCore(Size availableSize) 
at System.Windows.UIElement.Measure(Size availableSize) 
at System.Windows.Controls.Decorator.MeasureOverride(Size constraint) 
at System.Windows.FrameworkElement.MeasureCore(Size availableSize) 
at System.Windows.UIElement.Measure(Size availableSize) 
at System.Windows.Controls.Grid.MeasureCell(Int32 cell, Boolean forceInfinityV) 
at System.Windows.Controls.Grid.MeasureCellsGroup(Int32 cellsHead, Size referenceSize, Boolean ignoreDesiredSizeU, Boolean forceInfinityV, Boolean& hasDesiredSizeUChanged) 
at System.Windows.Controls.Grid.MeasureCellsGroup(Int32 cellsHead, Size referenceSize, Boolean ignoreDesiredSizeU, Boolean forceInfinityV) 
at System.Windows.Controls.Grid.MeasureOverride(Size constraint) 
at System.Windows.FrameworkElement.MeasureCore(Size availableSize) 
at System.Windows.UIElement.Measure(Size availableSize) 
at System.Windows.Controls.Grid.MeasureOverride(Size constraint) 
at System.Windows.FrameworkElement.MeasureCore(Size availableSize) 
at System.Windows.UIElement.Measure(Size availableSize) 
at MS.Internal.Helper.MeasureElementWithSingleChild(UIElement element, Size constraint) 
at System.Windows.Controls.ContentPresenter.MeasureOverride(Size constraint) 
at System.Windows.FrameworkElement.MeasureCore(Size availableSize) 
at System.Windows.UIElement.Measure(Size availableSize) 
at System.Windows.Controls.Grid.MeasureOverride(Size constraint) 
at System.Windows.FrameworkElement.MeasureCore(Size availableSize) 
at System.Windows.UIElement.Measure(Size availableSize) 
at System.Windows.Window.MeasureOverrideHelper(Size constraint) 
at System.Windows.Window.MeasureOverride(Size availableSize) 
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 System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler) 

--Inner Exception: 
System.InvalidOperationException: Specified element is already the logical child of another element. Disconnect it first. 
at System.Windows.FrameworkElement.ChangeLogicalParent(DependencyObject newParent) 
at System.Windows.FrameworkElement.AddLogicalChild(Object child) 
at System.Windows.Controls.HeaderedItemsControl.OnHeaderChanged(Object oldHeader, Object newHeader) 
at System.Windows.Controls.HeaderedItemsControl.OnHeaderChanged(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) 

ContextMenuリソース:

<ContextMenu x:Key="QuestionContextMenu"> 
    <MenuItem Command="{Binding AddChildCommand}" 
       CommandParameter="{Binding Model}" 
       Header="{StaticResource s_Question_Tooltip_AddChildAnswer}" /> 
    <MenuItem Command="{Binding RemoveCommand}" 
       CommandParameter="{Binding Model}" 
       Header="{StaticResource s_Remove}" /> 
</ContextMenu> 

UserControl.Resourcesタグ内)リソース:XAMLで

<DataTemplate x:Key="dt_Questions" 
       DataType="secure:QuestionViewModel"> 
    <Grid x:Name="MainGrid" 
      Width="{Binding ElementName=lvQuestions, Path=ActualWidth}" 
      MinHeight="22" 
      ContextMenu="{StaticResource QuestionContextMenu}" 
      Margin="0 2 0 2"> 
     ... 
    </Grid> 
</DataTemplate> 
ListBox

<ItemsPanelTemplate x:Key="VirtualizingItemsPanelTemplate"> 
    <VirtualizingStackPanel /> 
</ItemsPanelTemplate> 
<Style x:Key="LeftListBox" 
     TargetType="{x:Type ListBox}"> 
    <Setter Property="OverridesDefaultStyle" Value="True" /> 
    <Setter Property="Padding" Value="0" /> 
    <Setter Property="Background" Value="{x:Null}" /> 
    <Setter Property="ItemsPanel" Value="{StaticResource VirtualizingItemsPanelTemplate}" /> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type ListBox}"> 
       <ScrollViewer Background="{TemplateBinding Background}" 
              CanContentScroll="True" 
              Style="{StaticResource LeftScrollViewerStyle}"> 
        <ItemsPresenter Margin="{TemplateBinding Padding}" /> 
       </ScrollViewer> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

ListBoxItemテンプレート:

<Style TargetType="ListBoxItem"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="ListBoxItem"> 
       <ContentPresenter /> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 
テンプレートと

<ListBox x:Name="lvQuestions" 
     Grid.Row="1" 
     Grid.Column="0" 
     Grid.ColumnSpan="2" 
     Padding="0 2 0 20" 
     Panel.ZIndex="{Binding QuestionsZindex}" 
     ItemTemplate="{StaticResource dt_Questions}" 
     ItemsSource="{Binding QuestionsView, Mode=OneWay}" 
     SelectedItem="{Binding SelectedQuestionViewModel, Mode=TwoWay}" 
     SelectionMode="Single" 
     Style="{StaticResource LeftListBox}" 
     VirtualizingStackPanel.IsVirtualizing="True" 
     VirtualizingStackPanel.ScrollUnit="Pixel" 
     VirtualizingStackPanel.VirtualizationMode="Recycling" /> 
ListBox

スタイル

私の考え:ContextMenuが間違っているか、.NET 4.5のScrollUnit="Pixel"に問題があります。

PS:問題の原因がContextMenuであることを確認するのに多くの時間を費やしました。私はその問題を解決するためにXAMLマークアップを何とか作り直そうとしましたが、まだ出現しています。私は仮想化を無効にしたり、自分でContextMenuのコントロールを作ったりしたくありません。私はその問題の回避策を探したい。

答えて

0

見つかり答え:

例外のソースがContextMenuではなかった、それは私のローカライゼーション値にStaticResourceとして設定Header ropertyとMenuItemました。 この値リソースは、Stringではなく、TextBlockでした。

されました:

<TextBlock x:Key="s_Question_Tooltip_AddChildAnswer"> 
    Add answer to this question 
<TextBlock> 

修正:

<s:String x:Key="s_Question_Tooltip_AddChildAnswer"> 
    Add answer to this question 
</s:String>