InvalidOperationException TrySimpleTransformToAncestor

Nov 14, 2008 at 9:47 AM
Hi,
i have a testapp with a datagrid and sometimes i got the following error. i bind the grid to a datatable at runtime in a callback method. it works fine, but sometimes this error happens.

System.InvalidOperationException: Das angegebene Visual-Objekt ist kein übergeordnetes Element dieses Visual-Objekts.
   bei System.Windows.Media.Visual.TrySimpleTransformToAncestor(Visual ancestor, Boolean inverse, GeneralTransform& generalTransform, Matrix& simpleTransform)
   bei System.Windows.Media.Visual.InternalTransformToAncestor(Visual ancestor, Boolean inverse)
   bei System.Windows.Media.Visual.TransformToAncestor(Visual ancestor)
   bei Microsoft.Windows.Controls.DataGridCellsPanel.ComputeCellsPanelHorizontalOffset()
   bei Microsoft.Windows.Controls.DataGridHelper.GetParentCellsPanelHorizontalOffset(IProvideDataGridColumn cell)
   bei Microsoft.Windows.Controls.DataGrid.InvalidateCellsPanelHorizontalOffset(Object args)
   bei System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Boolean isSingleParameter)
   bei System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)

my grid:
 <toolkit:DataGrid Grid.Row="0" Name="dataGrid2"
                              ItemsSource="{Binding}"
                              HorizontalGridLinesBrush="Transparent"
                                DisplayMemberPath="SOLL"
                                Visibility="Collapsed"
                                Background="Transparent"
                                AutoGenerateColumns="False"
                                AlternatingRowBackground="Beige"
                                RowDetailsVisibilityMode="VisibleWhenSelected"
                                >
                <toolkit:DataGrid.RowDetailsTemplate>
                    <DataTemplate>
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition />
                                <RowDefinition />
                            </Grid.RowDefinitions>
                            <StackPanel Orientation="Horizontal" Margin="20,0,0,0" Grid.Row="0">
                                <TextBlock Text="Buchungsstelle: " FontWeight="Bold"/>
                                <TextBlock Text="{Binding FORMAT_BKZ}"></TextBlock>
                            </StackPanel>
                            <StackPanel Orientation="Horizontal" Margin="20,0,0,0" Grid.Row="1">
                                <TextBlock Text="Verwendunszweck: " FontWeight="Bold"/>
                                <TextBlock Text="{Binding VERWEND}"></TextBlock>
                            </StackPanel>
                        </Grid>
                    </DataTemplate>
                </toolkit:DataGrid.RowDetailsTemplate>

                <toolkit:DataGrid.GroupStyle>
                    <GroupStyle>
                        <GroupStyle.Panel>
                            <ItemsPanelTemplate>
                                <Primitives:DataGridRowsPresenter />
                            </ItemsPanelTemplate>
                        </GroupStyle.Panel>
                        <GroupStyle.HeaderTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Path=Name}" FontWeight="Bold" Padding="3"/>
                            </DataTemplate>
                        </GroupStyle.HeaderTemplate>

                        <!--Der schicke Expander zum auf und zu klappen, bringt leider ab und an fehler-->
                        <GroupStyle.ContainerStyle>
                            <Style TargetType="{x:Type GroupItem}">
                                <Setter Property="Template">
                                    <Setter.Value>
                                        <ControlTemplate TargetType="{x:Type GroupItem}">
                                            <Expander IsExpanded="False">
                                                <Expander.Header>
                                                    <DockPanel TextBlock.FontWeight="Bold">
                                                        <TextBlock Name="txt1" Text="{Binding Path=Name}" Margin="5,0,5,0" />
                                                        <TextBlock Name="txt3" Text="{Binding Path=ItemCount}"/>
                                                    </DockPanel>
                                                </Expander.Header>
                                                <ItemsPresenter />
                                            </Expander>
                                        </ControlTemplate>
                                    </Setter.Value>
                                </Setter>
                            </Style>
                        </GroupStyle.ContainerStyle>
                    </GroupStyle>
                </toolkit:DataGrid.GroupStyle>
                <toolkit:DataGrid.Columns>
                    <toolkit:DataGridTextColumn Header="Name"
                                                Binding="{Binding SOLL_NAME}"
                                                IsReadOnly="True"
                                                MinWidth="200"/>
                    <toolkit:DataGridTextColumn Header="PK-NR"
                                                Binding="{Binding BKZ}"
                                                IsReadOnly="True"
                                                MinWidth="125"/>
                    <toolkit:DataGridTextColumn Header="PKART"
                                                Binding="{Binding PKART}"
                                                IsReadOnly="True"
                                                MinWidth="75"/>
                    <toolkit:DataGridTextColumn Header="KBH"
                                                Binding="{Binding KBH}"
                                                IsReadOnly="True"
                                                MinWidth="75"/>
                    <toolkit:DataGridTextColumn Header="AOST-NR."
                                                Binding="{Binding FORMAT_AOST}"
                                                IsReadOnly="True"
                                                MinWidth="125"/>
                </toolkit:DataGrid.Columns>
            </toolkit:DataGrid>


do i miss something?
Coordinator
Nov 14, 2008 at 9:54 PM
I believe it is related to the GroupItem not being in the VisualTree.  I saw this recently in one of my apps and it was related to my Expander.IsExpanded in the GroupItem being bound to my ViewModel and causing a binding operation to occur too soon and collapsing everything.  I was able to fix it but I don't think that is your problem here.  How are you setting your GroupDescriptions?
Nov 18, 2008 at 5:22 AM
Hi Vin,
since i have the expander as my <GroupStyle.ContainerStyle> this error happens sometimes. you wrote you was able to fix your Expander problem, maybe it will solve my problem too?

here my GroupDescriptions.
private void set_grouping(string columnname)
{
            ICollectionView view = CollectionViewSource.GetDefaultView(this.dataGrid2.ItemsSource);
            if (view != null && view.CanGroup)
            {
                view.GroupDescriptions.Clear();
                view.GroupDescriptions.Add(new PropertyGroupDescription(columnname));
            }
 }

Nov 21, 2008 at 5:12 AM
Edited Dec 1, 2008 at 9:43 AM
Hi Vin, i hope you have some time to look for this issue?
thx Frank

Dec 1, 2008 at 9:44 AM
*push*

still have this problem as long as my <GroupStyle.ContainerStyle> is an expander.
Coordinator
Dec 3, 2008 at 12:59 PM
Set the IsExpanded to true.  That was what solved my issue.
Dec 4, 2008 at 5:18 AM
oh thats all? :) thank's a lot Vin!

ciao Frank

ps:  is this a bug in wpf?
Jan 14, 2009 at 11:40 PM
Hi - I've just encountered this issue as well:  I have a grouping applied in my DataGrid.  If all groups are expanded when I add another group then all as well.  But if I have a mixture, some collapsed and some expanded, and then I add a group, I will get the exception below.  The same exception occurs if I have multiple groups applied, some collapsed and some expanded, and then I remove a group.  

I can get around the exception by setting IsExpanded="True" in my DataGrid GroupItem ControlTemplate.  But having all groups expanded by default is not desirable...  Is there a workaround?  Is this a bug that will be addressed?

Thanks,
E.


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.InternalTransformToAncestor(Visual ancestor, Boolean inverse)
       at System.Windows.Media.Visual.TransformToAncestor(Visual ancestor)
       at Microsoft.Windows.Controls.DataGridCellsPanel.ComputeCellsPanelHorizontalOffset()
       at Microsoft.Windows.Controls.DataGridHelper.GetParentCellsPanelHorizontalOffset(IProvideDataGridColumn cell)
       at Microsoft.Windows.Controls.DataGrid.InvalidateCellsPanelHorizontalOffset(Object args)
       at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Boolean isSingleParameter)
       at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
       at System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
       at System.Windows.Threading.DispatcherOperation.InvokeImpl()
       at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state)
       at System.Threading.ExecutionContext.runTryCode(Object userData)

Jan 16, 2009 at 6:20 PM
For completeness (in case anyone looks up this thread), I ended up circumventing this issue by programmatically collapsing all groups in my DataGrid before adding a new group or removing an existing one.


Mar 3, 2009 at 4:26 PM
I ran into this issue also.  In my scenarios, we have to recreate the DataGrid.ItemsSource a lot and by doing that the crash of "The specified Visual is not an ancestor of this Visual" is un-avoidable.  I've tried many things including all of the solutions mentioned above, but the DataGridCellsPanel.ComputeCellsPanelHorizontalOffset() keeps throw the exception.  I even tried to not recreate the ItemsSource but to swap items within the container instead.  It's still throwing exception at random especially in the case of removing the last item.

My workaround is to download the source code of the WPF toolkit (Jan 30 2009), fix the DataGridCellsPanel.ComputeCellsPanelHorizontalOffset() as below:

internal double ComputeCellsPanelHorizontalOffset()
{
            Debug.Assert(ParentDataGrid != null, "ParentDataGrid should not be null");

            double cellsPanelOffset = 0.0;
            DataGrid dataGrid = ParentDataGrid;
            double horizontalOffset = dataGrid.HorizontalScrollOffset;
            ScrollViewer scrollViewer = dataGrid.InternalScrollHost;
            // Add a check to see if the scrollViewer is the ancestor of this cell
            // before doing the transform.  Otherwise, the transform will throw
            // "The specified Visual is not an ancestor of this visual".
            // Hope that Microsoft will fix this issue in next release.  -DeTran
            if (scrollViewer != null && scrollViewer.IsAncestorOf(this))
            {
                cellsPanelOffset = horizontalOffset + TransformToAncestor(scrollViewer).Transform(new Point()).X;
            }

            return cellsPanelOffset;
}

Good Luck,
-De Tran

Apr 7, 2009 at 5:52 PM
Edited Apr 7, 2009 at 6:11 PM
Also hit this, can someone please add the fix to the next release.

Added an issue on it.  http://wpf.codeplex.com/WorkItem/View.aspx?WorkItemId=9699
Apr 7, 2009 at 6:27 PM
I also see this. Just voted it up.

Coordinator
Apr 10, 2009 at 8:14 PM
Thanks for reporting this issue; we are actively investigating and hope to release a fix in a future toolkit update.

Alexis
Jun 19, 2009 at 1:47 PM

Nice Job De Tran, your change worked for me. Thanks.

About when should we expect an official fix on this?

Coordinator
Jul 10, 2009 at 1:19 AM

Could any of you facing this issue send a small app which shows this issue and give steps to consistently reproduce this issue ? You can attach the app at this link http://wpf.codeplex.com/WorkItem/View.aspx?WorkItemId=9699.

Thanks.