WPF中绑定的Markup.IStyle.Connector.Connect错误 [英] Markup.IStyle.Connector.Connect error on binding in WPF

查看:152
本文介绍了WPF中绑定的Markup.IStyle.Connector.Connect错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好时光
我在WPF中的数据绑定有问题。
如果我使用绑定:

 < TextBlock Text ={Binding Key}TextTrimming =CharacterEllipsis
MaxWidth ={Binding ActualWidth,RelativeSource = {RelativeSource
AncestorType = {x:Type Expander}},Converter = {StaticResource string_cutter_width}}/>

在下面的代码中我有一个 Markup.IStyle.Connector.Connect 错误在一些电脑上。
我不明白为什么
如果你遇到这样的行为或有一些想法。请分享。









 < DataGridTemplateColumn Header ={x:Static res:Resources.status_col_header}Width =1 *> 
< DataGridTemplateColumn.CellTemplate>
< DataTemplate>
< DataTemplate.Resources>
< local:Localizer2 x:Key =Localizer2/>
< /DataTemplate.Resources>
< Expander Header ={Binding status.Text,Converter = {StaticResource Localizer2}}MouseEnter =Expander_MouseEnterTag ={Binding}>
< TreeView x:Name =GTreeItemsSource ={Binding status.files}ScrollViewer.Horizo​​ntalScrollBarVisibility =Hidden>
< TreeView.ItemTemplate>
< DataTemplate>
< StackPanel Orientation =Horizo​​ntalClipToBounds =FalseIsHitTestVisible =trueMargin =1>
< StackPanel Orientation =Vertical>
< StackPanel.Resources>
< local:string_cutter_width x:Key =string_cutter_width/>
< /StackPanel.Resources>
< TextBlock Text ={Binding Key}TextTrimming =CharacterEllipsisMaxWidth ={Binding ActualWidth,RelativeSource = {RelativeSource AncestorType = {x:Type Expander}},Converter = {StaticResource string_cutter_width}} >
< TextBlock Text ={Binding Value}TextTrimming =CharacterEllipsisMaxWidth ={Binding ActualWidth,RelativeSource = {RelativeSource AncestorType = {x:Type Expander}},Converter = {StaticResource string_cutter_width}} >
<! - < TextBlock Text ={Binding Key}TextTrimming =CharacterEllipsis/>
< TextBlock Text ={Binding Value}TextTrimming =CharacterEllipsis/> - >
< / StackPanel>
< StackPanel Orientation =VerticalHorizo​​ntalAlignment =RightVerticalAlignment =Center>
< StackPanel.Resources>
< local:del_label_converter x:Key =del_label_converter/>
< /StackPanel.Resources>
<! - < Image Source =Resources\delete_16.pngStretch =UniformToFillPreviewMouseUp =Image_MouseUpWidth =16Height =16Tag ={Binding} ={Binding IsEnabled,ElementName = start_btn,Converter = {StaticResource del_label_converter}}/> - >
< Image Source =Resources\delete_16.pngStretch =UniformToFillPreviewMouseUp =Image_MouseUpWidth =16Height =16Tag ={Binding}/>
< / StackPanel>
< / StackPanel>
< / DataTemplate>
< /TreeView.ItemTemplate>
< / TreeView>
< / Expander>
< / DataTemplate>
< /DataGridTemplateColumn.CellTemplate>
< / DataGridTemplateColumn>
< /DataGrid.Columns>

< DataGrid.ContextMenu>
< ContextMenu>
< MenuItem Header =ПроверкаподключенияCamDriveClick =MenuItem_ClickIsEnabled ={Binding Path = camdrive_can_be_chk_manually}/>
<! - < MenuItem Header =ПерезагрузкакамерыClick =RebootCamera_Click/> - >
< / ContextMenu>
< /DataGrid.ContextMenu>
< / DataGrid>

调用堆栈:

  Camdrive.UpgradeTools.MainWindow.System.Windows.Markup.IStyleConnector.Connect(Int32 connectionId,Object target)вc:\Develop\Camdrive.Utils\Camdrive.UpgradeTools\Camdrive.UpgradeTools \MainWindow.xaml:строка75 
ÂSystem.Windows.FrameworkTemplate.LoadTemplateXaml(XamlReader templateReader,XamlObjectWriter currentWriter)
вSystem.Windows.FrameworkTemplate.LoadTemplateXaml(XamlObjectWriter objectWriter)
вSystem。 Windows.FrameworkTemplate.LoadOptimizedTemplateContent(DependencyObject container,IComponentConnector componentConnector,IStyleConnector styleConnector,List`1 affectedChildren,UncommonField`1 templatedNonFeChildrenField)
System.Windows.FrameworkTemplate.LoadContent(DependencyObject容器,List`1 affectedChildren)
вSystem.Windows.StyleHelper.ApplyTemplateContent(UncommonField`1 dataField,DependencyObject container,FrameworkElementF actory templateRoot,Int32 lastChildIndex,HybridDictionary childIndexFromChildID,FrameworkTemplate frameworkTemplate)
вSystem.Windows.FrameworkTemplate.ApplyTemplateContent(UncommonField`1 templateDataField,FrameworkElement container)
вSystem.Windows.FrameworkElement.ApplyTemplate()
вSystem.Windows.FrameworkElement.MeasureCore(Size availableSize)
ÂSystem.Windows.UIElement.Measure(Size availableSize)
вMS.Internal.Helper.MeasureElementWithSingleChild(UIElement element,Size constraint)
вSystem.Windows.Controls.ContentPresenter.MeasureOverride(Size constraint)
ÂSystem.Windows.FrameworkElement.MeasureCore(Size availableSize)
ÂSystem.Windows.UIElement.Measure(Size availableSize)
вSystem.Windows.Controls.Border.MeasureOverride(Size constraint)
ÂSystem.Windows.FrameworkElement.MeasureCore(Size availableSize)
ÂSystem.Windows.UIElement.Measure(Size availableSize)
System.Windows.Controls.Control.MeasureOverri de(Size constraint)
ÂSystem.Windows.Controls.DataGridCell.MeasureOverride(Size constraint)
ÂSystem.Windows.FrameworkElement.MeasureCore(Size availableSize)
вSystem.Windows.UIElement。测量(Size availableSize)
вSystem.Windows.Controls.DataGridCellsPanel.MeasureChild(UIElement child,Size constraint)
вSystem.Windows.Controls.DataGridCellsPanel.GenerateChild(IItemContainerGenerator Generator,Size constraint,DataGridColumn column, Int32& childIndex,Size& childSize)
ÂSystem.Windows.Controls.DataGridCellsPanel.GenerateChildren(IItemContainerGenerator生成器,Int32 startIndex,Int32 endIndex,Size约束)
ÂSystem.Windows.Controls.DataGridCellsPanel.GenerateAndMeasureChildrenForRealizedColumns(Size constraint)
System.Windows.Controls.DataGridCellsPanel.MeasureOverride(Size constraint)
ÂSystem.Windows.FrameworkElement.MeasureCore(Size availableSize)
ÂSystem.Windows.UIElement.Measure(Size availableSize)
вMS.Internal.Helper.MeasureElementWithSingleChild(UIElement element,Size constraint)
ÂSystem.Windows.Controls.ItemsPresenter.MeasureOverride(Size constraint)
вSystem.Windows.FrameworkElement.MeasureCore(Size availableSize)
вSystem.Windows.UIElement.Measure(Size availableSize)
вSystem.Windows.Controls.Control.MeasureOverride(Size constraint)
вSystem.Windows.Controls.Primitives.DataGridCellsPresenter.MeasureOverride (Size availableSize)
вSystem.Windows.F rameworkElement.MeasureCore(Size availableSize)
ÂSystem.Windows.UIElement.Measure(Size availableSize)
вSystem.Windows.Controls.Grid.MeasureCell(Int32 cell,Boolean forceInfinityV)
в系统.Windows.Controls.Grid.MeasureCellsGroup(Int32 cellsHead,Size referenceSize,Boolean ignoreDesiredSizeU,Boolean forceInfinityV)
вSystem.Windows.Controls.Grid.MeasureOverride(Size constraint)
ÂSystem.Windows.FrameworkElement。 MeasureCore(Size availableSize)
ÂSystem.Windows.UIElement.Measure(Size availableSize)
ÂSystem.Windows.Controls.Border.MeasureOverride(Size constraint)
ÂSystem.Windows.FrameworkElement。 MeasureCore(Size availableSize)
ÂSystem.Windows.UIElement.Measure(Size availableSize)
вSystem.Windows.Controls.Control.MeasureOverride(Size constraint)
ÂSystem.Windows.FrameworkElement。 MeasureCore(Size availableSize)
ÂSystem.Windows.UIElement.Measure(Size availableSize)
вSystem.Windows.Controls.VirtualizingStac kPanel.MeasureOverride(Size约束)
ÂSystem.Windows.Controls.Primitives.DataGridRowsPresenter.MeasureOverride(Size constraint)
ÂSystem.Windows.FrameworkElement.MeasureCore(Size availableSize)
вSystem。 Windows.UIElement.Measure(Size availableSize)
ÂSystem.Windows.ContextLayoutManager.UpdateLayout()
ÂSystem.Windows.ContextLayoutManager.UpdateLayoutCallback(Object arg)
ÂSystem.Windows.Media。 MediaContext.InvokeOnRenderCallback.DoWork()
System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks()
ÂSystem.Windows.Media.MediaContext.RenderMessageHandlerCore(Object resizedCompositionTarget)
ÂSystem.Windows。 Media.MediaContext.RenderMessageHandler(Object resizedCompositionTarget)
вSystem.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback,Object args,Int32 numArgs)
вMS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source ,Delegate方法,Object args,Int32 numArgs,Delegate ca tchHandler)
вSystem.Windows.Threading.DispatcherOperation.InvokeImpl()
вSystem.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state)
вSystem.Threading.ExecutionContext.runTryCode(Object userData)
System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode代码,CleanupCode backoutCode,Object userData)
System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext,ContextCallback callback,Object state)
вSystem.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback callback,Object state,Boolean ignoreSyncCtx)
вSystem.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback callback,Object state)
в System.Windows.Threading.DispatcherOperation.Invoke()
ÂSystem.Windows.Threading.Dispatcher.ProcessQueue()
вSystem.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd,Int32 msg,IntPtr wParam ,IntPtr lParam,Boolean&处理)
вMS.Win32.HwndWrapper.WndProc(IntPtr hwnd,Int32 msg,IntPtr wParam,IntPtr lParam,Boolean&被处理)
вMS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback,Object args,Int32 numArgs)
вMS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source,Delegate method,Object args,Int32 numArgs,Delegate catchHandler )
вSystem.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority,TimeSpan timeout,Delegate method,Object args,Int32 numArgs)
вMS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd,Int32 msg, IntPtr wParam,IntPtr lParam)
вMS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
ÂSystem.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame框架)
ÂSystem.Windows。 Threading.Dispatcher.PushFrame(DispatcherFrame frame)
вSystem.Windows.Application.RunDispatcher(Object ignore)
вSy (Windows窗口)
System.Windows.Application.Run(窗口窗口)
System.Windows.Application.Run()
çCamdrive.UpgradeTools。 App.Main()C:\Develop\Camdrive.Utils\Camdrive.UpgradeTools\Camdrive.UpgradeTools\obj\x86\Debug\App.g.cs:строка0
в System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly,String [] args)
ÂSystem.AppDomain.ExecuteAssembly(String assemblyFile,Evidence assemblySecurity,String [] args)
вMicrosoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly ()
ÂSystem.Threading.ThreadHelper.ThreadStart_Context(Object state)
вSystem.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback callback,Object state,Boolean ignoreSyncCtx)
вSystem。 Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback callback,Object state)
вSystem.Threading.ThreadHelper.ThreadStart()


解决方案

很可能是因为嵌套模板:



CellTemplate - > DataTemplate - > ... - > TreeView - > TreeView.ItemTemplate - > DataTemplate



引用 this


此错误需要模板中的模板。查看此问题的一个好方法模板就像括号,引用括号模板XAML未创建,但保存并稍后运行。因此,错误是:我们在嵌套括号中出现问题。


在示例中:

 < Grid> 
< ListBox ItemsSource ={StaticResource Data}>
< ListBox.ItemTemplate>
< DataTemplate>
< ListBox ItemsSource ={Binding。}>
< ListBox.ItemTemplate>
< DataTemplate>
< TextBlock Text ={Binding。}Width =30
Loaded =TextBlock_Loaded
Style ={StaticResource TextBlockStyle}/>
< / DataTemplate>
< /ListBox.ItemTemplate>
< / ListBox>
< / DataTemplate>
< /ListBox.ItemTemplate>
< / ListBox>
< / Grid>

内部模板仍然是引用外部模板被应用。然而,我们仍然为所有的XAML调用IStyleConnector.Connect()(当我们点击内括号/ quote 时不停止),并且没有TextBlock实例,所以我们崩溃



由于优化的模板共享,怪异发生在重现。例如,如果您删除:

  Style ={StaticResource TextBlockStyle}

该错误消失,因为TextBlock在模板优化器中变为可共享



WorkAround:



最好的工作是将内部模板移动到资源中。



尝试在资源中移动您的 TreeView.ItemTemplate

Good time. I have a problem with data binding in WPF. If i use binding:

<TextBlock Text="{Binding Key}" TextTrimming="CharacterEllipsis" 
MaxWidth="{Binding ActualWidth, RelativeSource={RelativeSource 
AncestorType={x:Type Expander}}, Converter={StaticResource string_cutter_width}}"/>

in code below I have a Markup.IStyle.Connector.Connect error on some computers. I can't understand why. If you met such behavior or have some ideas about it. PLEASE SHARE.

            <DataGridTemplateColumn Header="{x:Static res:Resources.status_col_header}" Width="1*">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <DataTemplate.Resources>
                            <local:Localizer2 x:Key="Localizer2" />
                        </DataTemplate.Resources>
                        <Expander Header="{Binding status.Text, Converter={StaticResource Localizer2}}" MouseEnter="Expander_MouseEnter" Tag="{Binding}">
                            <TreeView x:Name="GTree" ItemsSource="{Binding status.files}" ScrollViewer.HorizontalScrollBarVisibility="Hidden">
                                <TreeView.ItemTemplate>
                                    <DataTemplate>
                                        <StackPanel Orientation="Horizontal" ClipToBounds="False" IsHitTestVisible="true" Margin="1">
                                            <StackPanel Orientation="Vertical">
                                                <StackPanel.Resources>
                                                    <local:string_cutter_width x:Key="string_cutter_width" />
                                                </StackPanel.Resources>
                                                <TextBlock Text="{Binding Key}" TextTrimming="CharacterEllipsis" MaxWidth="{Binding ActualWidth, RelativeSource={RelativeSource AncestorType={x:Type Expander}}, Converter={StaticResource string_cutter_width}}"/>
                                                <TextBlock Text="{Binding Value}" TextTrimming="CharacterEllipsis" MaxWidth="{Binding ActualWidth, RelativeSource={RelativeSource AncestorType={x:Type Expander}}, Converter={StaticResource string_cutter_width}}"/>
                                                <!--<TextBlock Text="{Binding Key}" TextTrimming="CharacterEllipsis"/>
                                                <TextBlock Text="{Binding Value}" TextTrimming="CharacterEllipsis"/>-->
                                            </StackPanel >
                                            <StackPanel Orientation="Vertical" HorizontalAlignment="Right" VerticalAlignment="Center">
                                                <StackPanel.Resources>
                                                    <local:del_label_converter x:Key="del_label_converter" />
                                                </StackPanel.Resources>
                                                <!--<Image Source="Resources\delete_16.png" Stretch="UniformToFill" PreviewMouseUp="Image_MouseUp" Width="16" Height="16" Tag="{Binding}" Visibility="{Binding IsEnabled, ElementName=start_btn, Converter={StaticResource del_label_converter} }"/>-->
                                                <Image Source="Resources\delete_16.png" Stretch="UniformToFill" PreviewMouseUp="Image_MouseUp" Width="16" Height="16" Tag="{Binding}"/>
                                            </StackPanel>
                                        </StackPanel>
                                    </DataTemplate>
                                </TreeView.ItemTemplate>
                            </TreeView>
                        </Expander>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>

        <DataGrid.ContextMenu>
            <ContextMenu>
                <MenuItem Header="Проверка подключения CamDrive" Click="MenuItem_Click" IsEnabled="{Binding Path=camdrive_can_be_chk_manually}"/>
                <!--<MenuItem Header="Перезагрузка камеры" Click="RebootCamera_Click"/>-->
            </ContextMenu>
        </DataGrid.ContextMenu>
    </DataGrid>

call stack:

в Camdrive.UpgradeTools.MainWindow.System.Windows.Markup.IStyleConnector.Connect(Int32 connectionId, Object target) в c:\Develop\Camdrive.Utils\Camdrive.UpgradeTools\Camdrive.UpgradeTools\MainWindow.xaml:строка 75
в System.Windows.FrameworkTemplate.LoadTemplateXaml(XamlReader templateReader, XamlObjectWriter currentWriter)
в System.Windows.FrameworkTemplate.LoadTemplateXaml(XamlObjectWriter objectWriter)
в System.Windows.FrameworkTemplate.LoadOptimizedTemplateContent(DependencyObject container, IComponentConnector componentConnector, IStyleConnector styleConnector, List`1 affectedChildren, UncommonField`1 templatedNonFeChildrenField)
в System.Windows.FrameworkTemplate.LoadContent(DependencyObject container, List`1 affectedChildren)
в System.Windows.StyleHelper.ApplyTemplateContent(UncommonField`1 dataField, DependencyObject container, FrameworkElementFactory templateRoot, Int32 lastChildIndex, HybridDictionary childIndexFromChildID, FrameworkTemplate frameworkTemplate)
в System.Windows.FrameworkTemplate.ApplyTemplateContent(UncommonField`1 templateDataField, FrameworkElement container)
в System.Windows.FrameworkElement.ApplyTemplate()
в System.Windows.FrameworkElement.MeasureCore(Size availableSize)
в System.Windows.UIElement.Measure(Size availableSize)
в MS.Internal.Helper.MeasureElementWithSingleChild(UIElement element, Size constraint)
в System.Windows.Controls.ContentPresenter.MeasureOverride(Size constraint)
в System.Windows.FrameworkElement.MeasureCore(Size availableSize)
в System.Windows.UIElement.Measure(Size availableSize)
в System.Windows.Controls.Border.MeasureOverride(Size constraint)
в System.Windows.FrameworkElement.MeasureCore(Size availableSize)
в System.Windows.UIElement.Measure(Size availableSize)
в System.Windows.Controls.Control.MeasureOverride(Size constraint)
в System.Windows.Controls.DataGridCell.MeasureOverride(Size constraint)
в System.Windows.FrameworkElement.MeasureCore(Size availableSize)
в System.Windows.UIElement.Measure(Size availableSize)
в System.Windows.Controls.DataGridCellsPanel.MeasureChild(UIElement child, Size constraint)
в System.Windows.Controls.DataGridCellsPanel.GenerateChild(IItemContainerGenerator generator, Size constraint, DataGridColumn column, Int32& childIndex, Size& childSize)
в System.Windows.Controls.DataGridCellsPanel.GenerateChildren(IItemContainerGenerator generator, Int32 startIndex, Int32 endIndex, Size constraint)
в System.Windows.Controls.DataGridCellsPanel.GenerateAndMeasureChildrenForRealizedColumns(Size constraint)
в System.Windows.Controls.DataGridCellsPanel.MeasureOverride(Size constraint)
в System.Windows.FrameworkElement.MeasureCore(Size availableSize)
в System.Windows.UIElement.Measure(Size availableSize)
в MS.Internal.Helper.MeasureElementWithSingleChild(UIElement element, Size constraint)
в System.Windows.Controls.ItemsPresenter.MeasureOverride(Size constraint)
в System.Windows.FrameworkElement.MeasureCore(Size availableSize)
в System.Windows.UIElement.Measure(Size availableSize)
в System.Windows.Controls.Control.MeasureOverride(Size constraint)
в System.Windows.Controls.Primitives.DataGridCellsPresenter.MeasureOverride(Size availableSize)
в System.Windows.FrameworkElement.MeasureCore(Size availableSize)
в System.Windows.UIElement.Measure(Size availableSize)
в System.Windows.Controls.Grid.MeasureCell(Int32 cell, Boolean forceInfinityV)
в System.Windows.Controls.Grid.MeasureCellsGroup(Int32 cellsHead, Size referenceSize, Boolean ignoreDesiredSizeU, Boolean forceInfinityV)
в System.Windows.Controls.Grid.MeasureOverride(Size constraint)
в System.Windows.FrameworkElement.MeasureCore(Size availableSize)
в System.Windows.UIElement.Measure(Size availableSize)
в System.Windows.Controls.Border.MeasureOverride(Size constraint)
в System.Windows.FrameworkElement.MeasureCore(Size availableSize)
в System.Windows.UIElement.Measure(Size availableSize)
в System.Windows.Controls.Control.MeasureOverride(Size constraint)
в System.Windows.FrameworkElement.MeasureCore(Size availableSize)
в System.Windows.UIElement.Measure(Size availableSize)
в System.Windows.Controls.VirtualizingStackPanel.MeasureOverride(Size constraint)
в System.Windows.Controls.Primitives.DataGridRowsPresenter.MeasureOverride(Size constraint)
в System.Windows.FrameworkElement.MeasureCore(Size availableSize)
в System.Windows.UIElement.Measure(Size availableSize)
в System.Windows.ContextLayoutManager.UpdateLayout()
в System.Windows.ContextLayoutManager.UpdateLayoutCallback(Object arg)
в System.Windows.Media.MediaContext.InvokeOnRenderCallback.DoWork()
в System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks()
в System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object  resizedCompositionTarget)
в System.Windows.Media.MediaContext.RenderMessageHandler(Object resizedCompositionTarget)
в System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
в MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
в System.Windows.Threading.DispatcherOperation.InvokeImpl()
в System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state)
в System.Threading.ExecutionContext.runTryCode(Object userData)
в System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
в System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
в System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
в System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
в System.Windows.Threading.DispatcherOperation.Invoke()
в System.Windows.Threading.Dispatcher.ProcessQueue()
в System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr  wParam, IntPtr lParam, Boolean& handled)
в MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam,  Boolean& handled)
в MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
в System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object  args, Int32 numArgs)
в MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
в System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
в MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
в MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
в System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
в System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
в System.Windows.Application.RunDispatcher(Object ignore)
в System.Windows.Application.RunInternal(Window window)
в System.Windows.Application.Run(Window window)
в System.Windows.Application.Run()
в Camdrive.UpgradeTools.App.Main() в C:\Develop\Camdrive.Utils\Camdrive.UpgradeTools\Camdrive.UpgradeTools\obj\x86\Debug\App.g.cs:строка 0
в System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
в System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
в Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
в System.Threading.ThreadHelper.ThreadStart_Context(Object state)
в System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
в System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
в System.Threading.ThreadHelper.ThreadStart()

解决方案

Most likely it is because of nested templates:

CellTemplate -> DataTemplate -> ... -> TreeView -> TreeView.ItemTemplate -> DataTemplate

Quoting this:

This bug requires a Template in a Template. A good way to look at this problem "Templating is like parentheses, quoting parentheses" the Template XAML is not created but saved and run later. The bug is therefore: We have a problem with nested parentheses.

In the sample:

<Grid>
    <ListBox ItemsSource="{StaticResource Data}" >
        <ListBox.ItemTemplate>
            <DataTemplate>
                <ListBox ItemsSource="{Binding .}">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding .}" Width="30"
                                     Loaded="TextBlock_Loaded"
                                     Style="{StaticResource TextBlockStyle}" />
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Grid>

The inner template is still quoted when the outer template is applied. Yet we still call IStyleConnector.Connect() for all the XAML (we don’t stop when we hit the inner parentheses/quote) and there is no TextBlock instance so we crash.

Weirdness happens with the repro because of optimized template sharing. For example if you remove the:

Style="{StaticResource TextBlockStyle}"

The bug goes away because the TextBlock becomes sharable in the Template optimizer.

WorkAround:

The best work around is move the inner template to a resource.

Try move your TreeView.ItemTemplate in resource.

这篇关于WPF中绑定的Markup.IStyle.Connector.Connect错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆