1

我有以下数据网格,但我不断收到错误消息。为什么 ?

   <TabControl Name="tabContent"  TabStripPlacement="Bottom">
                <TabItem Header="Information">
                    <DataGrid x:Name="gridInformation" AutoGenerateColumns="False">
                        <DataGrid.Columns>
                        <DataGridTemplateColumn Header="Address">                         
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <TextBlock  Text="{Binding Address}"></TextBlock>
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                            </DataGridTemplateColumn>
                        </DataGrid.Columns>                                                                     
                    </DataGrid>                                        
                </TabItem>               
            </TabControl>

这是错误

System.InvalidOperationException was unhandled
  Message=The specified Visual is not an ancestor of this Visual.
  Source=PresentationCore
  StackTrace:
       at System.Windows.Media.Visual.TrySimpleTransformToAncestor(Visual ancestor, Boolean inverse, GeneralTransform& generalTransform, Matrix& simpleTransform)
       at System.Windows.Media.Visual.TransformToAncestor(Visual ancestor)
       at System.Windows.Controls.DataGridCellsPanel.ComputeCellsPanelHorizontalOffset()
       at System.Windows.Controls.DataGridHelper.GetParentCellsPanelHorizontalOffset(IProvideDataGridColumn cell)
       at System.Windows.Controls.DataGrid.InvalidateCellsPanelHorizontalOffset(Object args)
       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)
       at System.Windows.Threading.DispatcherOperation.InvokeImpl()
       at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state)
       at System.Threading.ExecutionContext.runTryCode(Object userData)
       at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Windows.Threading.DispatcherOperation.Invoke()
       at System.Windows.Threading.Dispatcher.ProcessQueue()
       at System.Windows.Threading.Dispatcher.WndProcHook(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)
       at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
       at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
  InnerException: 

这是导致问题的主题部分:

                    <Grid Background="{TemplateBinding Background}" x:Name="Root">
                        <Grid.Resources>
                            <ControlTemplate x:Key="TopLeftHeaderTemplate" TargetType="DataGridColumnHeader">
                                <Grid Margin="1">
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="*" />
                                        <RowDefinition Height="*" />
                                        <RowDefinition Height="Auto" />
                                    </Grid.RowDefinitions>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="Auto" />
                                        <ColumnDefinition Width="*" />
                                        <ColumnDefinition Width="Auto" />
                                    </Grid.ColumnDefinitions>
                                    <Rectangle x:Name="Background" Opacity="0.65" Grid.ColumnSpan="3" Grid.RowSpan="3">
                                        <Rectangle.Fill>
                                            <LinearGradientBrush EndPoint="0.5,1.4" StartPoint="0.5,0">
                                                <GradientStop Color="{StaticResource PrimaryColor}" Offset="0.75" />
                                                <GradientStop Color="{StaticResource SecondaryColor}" Offset="1" />
                                            </LinearGradientBrush>
                                        </Rectangle.Fill>
                                    </Rectangle>
                                    <Border CornerRadius="0,0,40,40" x:Name="Highlight" RenderTransformOrigin="0.5,1" Grid.ColumnSpan="3">
                                        <Border.Background>
                                            <RadialGradientBrush>
                                                <RadialGradientBrush.RelativeTransform>
                                                    <TransformGroup>
                                                        <ScaleTransform CenterX="0.5" CenterY="0.5" ScaleX="1.25" ScaleY="2" />
                                                        <TranslateTransform Y="-0.6" />
                                                    </TransformGroup>
                                                </RadialGradientBrush.RelativeTransform>
                                                <GradientStop Color="#BFFFFFFF" Offset="0" />
                                                <GradientStop Color="#4CFFFFFF" Offset="1" />
                                            </RadialGradientBrush>
                                        </Border.Background>
                                    </Border>
                                </Grid>
                            </ControlTemplate>
                            <ControlTemplate x:Key="TopRightHeaderTemplate" TargetType="DataGridColumnHeader">
                                <Grid Margin="1">
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="*" />
                                        <RowDefinition Height="*" />
                                        <RowDefinition Height="Auto" />
                                    </Grid.RowDefinitions>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="Auto" />
                                        <ColumnDefinition Width="*" />
                                        <ColumnDefinition Width="Auto" />
                                    </Grid.ColumnDefinitions>
                                    <Rectangle x:Name="Background" Opacity="0.65" Grid.ColumnSpan="3" Grid.RowSpan="3">
                                        <Rectangle.Fill>
                                            <LinearGradientBrush EndPoint="0.5,1.4" StartPoint="0.5,0">
                                                <GradientStop Color="{StaticResource PrimaryColor}" Offset="0.75" />
                                                <GradientStop Color="{StaticResource SecondaryColor}" Offset="1" />
                                            </LinearGradientBrush>
                                        </Rectangle.Fill>
                                    </Rectangle>
                                    <Border CornerRadius="0,0,40,40" x:Name="Highlight" RenderTransformOrigin="0.5,1" Grid.ColumnSpan="3">
                                        <Border.Background>
                                            <RadialGradientBrush>
                                                <RadialGradientBrush.RelativeTransform>
                                                    <TransformGroup>
                                                        <ScaleTransform CenterX="0.5" CenterY="0.5" ScaleX="1.25" ScaleY="2" />
                                                        <TranslateTransform Y="-0.6" />
                                                    </TransformGroup>
                                                </RadialGradientBrush.RelativeTransform>
                                                <GradientStop Color="#BFFFFFFF" Offset="0" />
                                                <GradientStop Color="#4CFFFFFF" Offset="1" />
                                            </RadialGradientBrush>
                                        </Border.Background>
                                    </Border>
                                </Grid>
                            </ControlTemplate>
                        </Grid.Resources>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="*" />
                            <RowDefinition Height="Auto" />
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="Auto" />
                        </Grid.ColumnDefinitions>
                        <primitives:DataGridColumnHeader x:Name="TopLeftCornerHeader" Width="22" Template="{StaticResource TopLeftHeaderTemplate}" />
                        <primitives:DataGridColumnHeadersPresenter Grid.Column="1" x:Name="ColumnHeadersPresenter" />
                        <primitives:DataGridColumnHeader Grid.Column="2" x:Name="TopRightCornerHeader" Template="{StaticResource TopRightHeaderTemplate}" />
                        <Rectangle Grid.ColumnSpan="3" Height="1" x:Name="ColumnHeadersAndRowsSeparator" VerticalAlignment="Bottom" Width="Auto" Fill="#FFDBDCDC" StrokeThickness="1" />

                          <ScrollViewer   Grid.ColumnSpan="2" Grid.Row="1"  Padding="{TemplateBinding Padding}" Focusable="false" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" x:Name="scrollViewer">

                            <ItemsPresenter x:Name="ItemsPresenter" />

                          </ScrollViewer>
                        <Rectangle Grid.Column="2" Grid.Row="2" x:Name="BottomRightCorner" Fill="{TemplateBinding Background}" />
                        <Rectangle Grid.ColumnSpan="2" Grid.Row="2" x:Name="BottomLeftCorner" Fill="{TemplateBinding Background}" />                           
                        <Grid Grid.Column="1" Grid.Row="2">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>
                            <Rectangle x:Name="FrozenColumnScrollBarSpacer" />
                            <ScrollBar Grid.Column="1" Height="18" Margin="0" x:Name="HorizontalScrollbar" Orientation="Horizontal" Visibility="{TemplateBinding HorizontalScrollBarVisibility}"  />
                        </Grid>-->
                    </Grid>

                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
4

0 回答 0