2013-08-06 38 views
8

Thời gian tốt. Tôi gặp sự cố với ràng buộc dữ liệu trong WPF. Nếu tôi sử dụng ràng buộc:Markup.IStyle.Connector. Lỗi kết nối khi liên kết trong WPF

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

trong mã dưới đây tôi có một lỗi Markup.IStyle.Connector.Connect trên một số máy tính. Tôi không thể hiểu tại sao. Nếu bạn gặp hành vi như vậy hoặc có một số ý tưởng về nó. HÃY CHIA SẺ.

  <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> 

gọi 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() 

Trả lời

12

Nhiều khả năng đó là vì các mẫu lồng nhau:

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

Trích dẫn this:

lỗi này đòi hỏi một mẫu trong một Template. Một cách tốt để xem xét vấn đề này “Templating giống như dấu ngoặc đơn, trích dẫn ngoặc đơn” Template XAML không được tạo ra nhưng được lưu lại và chạy sau này. Do đó, lỗi: Chúng tôi gặp sự cố với các dấu ngoặc đơn lồng nhau.

Trong ví dụ:

<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> 

Mẫu nội vẫn là quoted khi mẫu ngoài được áp dụng. Tuy nhiên, chúng tôi vẫn gọi IStyleConnector.Connect() cho tất cả XAML (chúng tôi không dừng lại khi chúng tôi nhấn inner parentheses/quote) và không có trường hợp TextBlock để chúng tôi gặp sự cố.

Sự kỳ quặc xảy ra với repro vì chia sẻ mẫu được tối ưu hóa. Ví dụ: nếu bạn xóa:

Style="{StaticResource TextBlockStyle}" 

Lỗi này biến mất vì TextBlock trở thành sharable trong trình tối ưu hóa mẫu.

WorkAround:

Cách tốt nhất là di chuyển mẫu bên trong vào tài nguyên.

Hãy thử di chuyển TreeView.ItemTemplate trong tài nguyên của bạn.

+2

Cảm ơn bạn. Nó đã giúp tôi rất nhiều. –

+1

Có phiên bản .NET cụ thể nào có vấn đề này không? Tại sao một số máy thể hiện vấn đề này trong khi những máy khác thì không? Cách giải quyết và đề xuất của bạn hoạt động như một sự quyến rũ! –

+0

@Mike G: Phiên bản khuôn khổ nào trên máy? Lỗi này đã chuyển từ phiên bản '.NET 3.5' ​​sang phiên bản' .NET 4.0', chính xác cùng mã dưới 3.5 không có vấn đề gì. Có thể, trong phiên bản 4.5 nó đã được sửa, như đã hứa với Microsoft. Trong mọi trường hợp, như tôi nghĩ, nên tránh làm tổ mẫu và sử dụng thông qua tài nguyên. –

Các vấn đề liên quan