在WPF中绑定时出现Markup.IStyle.Connector.Connect错误

ies*_*ras 8 c# data-binding wpf

美好时光.我在WPF中遇到数据绑定问题.如果我使用绑定:

<TextBlock Text="{Binding Key}" TextTrimming="CharacterEllipsis" 
MaxWidth="{Binding ActualWidth, RelativeSource={RelativeSource 
AncestorType={x:Type Expander}}, Converter={StaticResource string_cutter_width}}"/>
Run Code Online (Sandbox Code Playgroud)

在下面的代码中,我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_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>
Run Code Online (Sandbox Code Playgroud)

调用堆栈:

? 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()
Run Code Online (Sandbox Code Playgroud)

Ana*_*aev 12

很可能是因为嵌套模板:

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

引用这个:

此错误需要模板中的模板.查看此问题的好方法"模板就像括号,引用括号"模板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>
Run Code Online (Sandbox Code Playgroud)

quoted应用外部模板时,内部模板仍然是.然而,我们仍然为所有XAML调用IStyleConnector.Connect()(当我们点击时我们不会停止inner parentheses/quote)并且没有TextBlock实例因此我们崩溃了.

由于优化的模板共享,repro发生了奇怪的事情.例如,如果您删除:

Style="{StaticResource TextBlockStyle}"
Run Code Online (Sandbox Code Playgroud)

该bug消失了,因为TextBlock变为sharable模板优化器.

WorkAround:

最好的解决方法是将内部模板移动到资源.

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

  • 谢谢.它对我帮助很大. (2认同)