Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Recursive call to Automation Peer API is not valid

Tags:

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.

like image 351
Ryan Avatar asked Oct 25 '10 18:10

Ryan


2 Answers

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.

like image 180
cunningdave Avatar answered Oct 17 '22 00:10

cunningdave


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.

like image 38
Protazy Avatar answered Oct 16 '22 23:10

Protazy