WPF Recursive call to Automation Peer API is not v

2019-01-15 18:21发布

问题:

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.

回答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.



回答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.



回答3:

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.



回答4:

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.



回答5:

I also had the same problem. So I checked also the reference to the WPFToolkit. I had two same versions of WPFToolkit installed (Version v3.5.50211.1), but only on my Laptop works it fine.

So I put the older version v3.5.40619.1 on my Windows Embedded Standard 7 PC and no more exceptions.

So I came to the conclusion that in some cases the newer Version has some problems with the runing system.



回答6:

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.