WPF Recursive call to Automation Peer API is not valid

15,495

Solution 1

I've bypassed the problem on my end by turning off Automation on the grid control. I found that the problem was unique to the WPF Toolkit control, but I was having problems transitioning to the 4.0 official release DataGrid (unrelated to this question.)

So instead, I derive the class from the WPFToolkit and supply this override:

protected override AutomationPeer OnCreateAutomationPeer()
{
   return null;
}

Maybe someone can tell us if this is a good idea or not.

Solution 2

I had exactly the same error. However for me it was strange that the same application was working fine on my laptop and caused the error on my desktop PC. The same OS, the same architecture and the same Visual Studio with the same add-ons.

So I checked references to WPFToolkit on my laptop, where everything was fine. It pointed to:

C:\Program Files (x86)\WPF Toolkit\v3.5.40619.1\WPFToolkit.dll

then I checked reference on my desktop, it pointed to:

C:\Program Files (x86)\WPF Toolkit\v3.5.50211.1\WPFToolkit.dll

As you can see I had two different versions of WPFToolkit installed. I copied whole folder from my laptop to my desktop, changed references from version v3.5.50211.1 to v3.5.40619.1 and the problem was resolved. No more exceptions. Hope this will help someone as well.

Solution 3

I was getting the same problem in NET 3.5 with WPFToolkit DataGrid.

I have bound my WPFToolkit DataGrid to EntityFramework ObservableCollection, with hierarchy of entities that have two way associations (Parent<->Items).

I solved the issue by disabling implicitly enabled AutoGenerateColumns on the DataGrid, and manually setting the columns.

Hope this helps.

Solution 4

I'm getting the same problem - are you using the datagrid from the WPFToolkit, or the one that ships with .NET 4.0. We're still using the toolkit one here.

Also, I've notice that this problem does not occur when using the app through remote desktop.

Similar problem posted here:

http://wpf.codeplex.com/workitem/14443

With a proposed solution. Haven't had a chance to try it.

Solution 5

Hi I also had same problem when I am running Microsoft Test Manager with our WPF application. We were using the WPFtoolkit version v3.5.50211.1, replacing WPF toolkit with lower version v3.5.40619.1 has solved this problem.

Now we are able to run the MTM tool and WPF application both simultaneously.

In WPFToolkit v3.5.50211.1 one bug is fixed related to UI Automation and I guess because of that this automation peer issue is coming while using the latest WPFtoolkit.

Share:
15,495
Ryan
Author by

Ryan

Updated on June 03, 2022

Comments

  • Ryan
    Ryan almost 2 years

    I am receiving an error message "Recursive call to Automation Peer API is not valid" when loading a datagrid with a datatemplatecolumn containing a combobox column. The error ends up caught in our unhandled exception code. This seems to be an issue on my machine, and google has provided no source of guidance on resolving the issue. The issue appears to only occur when I am populating the comboboxes with data. Populating the comboboxes (if I do not load data) works correctly, and while the error is displayed I am able to see the data properly retrieved in the background.

    I am using a WPF datagrid where I'm using a DataGridTemplateColumn for adding a combobox inside the grid. I have the drop down list bound to an enum using an objectdataprovider. In the code behind when initializing my screen I use a Linq2Sql statement to retrieve data and populate the Itemssource of the grid.

    <grid:DataGrid.Resources>
     <ObjectDataProvider
      x:Key="ChangeTypeData"
      MethodName="GetValues"
      ObjectType="{x:Type System:Enum}">
      <ObjectDataProvider.MethodParameters>
       <x:Type TypeName="namespace:ChangeType" />
      </ObjectDataProvider.MethodParameters>
     </ObjectDataProvider>     
        </grid:DataGrid.Resources>
    
     <grid:DataGrid.Columns>
     <grid:DataGridTextColumn Binding="{Binding DatapointName}" Header="Datapoint Changed" IsReadOnly="True" Width="Auto" />
     <grid:DataGridTemplateColumn Header="Change Type">
      <grid:DataGridTemplateColumn.CellTemplate>
       <DataTemplate>
        <ComboBox
         Text="{Binding Path=ChangeTypeName}"
         ItemsSource="{Binding Source={StaticResource ChangeTypeData}}"
         Name="dgcboChangeType"
    SelectionChanged="dgcboChangeType_SelectionChanged"/>
       </DataTemplate>
      </grid:DataGridTemplateColumn.CellTemplate>
    

    Any and all guidance on solving this issue is appreciated.

  • Ryan
    Ryan over 13 years
    I am using the WPFToolkit Datagrid.
  • lathander
    lathander about 13 years
    I can confirm that this workaround helps. I noticed that it is important not to include the part "assembly=..." in the xml namespace declaration if the workaround class is located in the same assembly as the XAML file (i.e. the assembly= part is redundant anyway). If it is present, the compiler will complain about not finding the xml tag, although IntelliSense works.
  • Abdullah Malikyar
    Abdullah Malikyar almost 9 years
    I am getting the same problem but its not in the DataGrid, I have a Combobox inside a Grid and sometimes when i try to click on to select a different item from the list it breaks and throws the same exception so i guess its a` combobox` problem, has anyone got any other solution.
  • si618
    si618 almost 8 years
    Sorry it's a year late @FaisalMalikyar, but I just hit this for a ComboBox as well. Solution appears to be the same - subclass the existing ComboBox, e.g. public class ComboBox : System.Windows.Controls.ComboBox, add code as above to new class, then add reference to your class in the XAML code, i.e. replacing the existing ComboBox with your own.