I have a large WPF application where I have a datagrid within a user control and I need to create an override for OnCreateAutomationPeer. I am having trouble doing that and the event never seems to fire. In my code-behind I have something similar to
public partial class DocChecklistView : UserControl, IDataModuleView {
protected override System.Windows.Automation.Peers.AutomationPeer OnCreateAutomationPeer()
{
return null;
}
public CDocumentChecklistView() {
InitializeComponent();
}
}
The XAML is pretty standard with code like
<UserControl>
<Grid>
<toolkit:DataGrid ItemsSource="{Binding Source={StaticResource DocumentsVS}}" AutoGenerateColumns="False" CanUserAddRows="False" CanUserDeleteRows="False"
FontSize="16" Name="_dgDocuments" Style="{StaticResource EklektosDataGridStyle}" . . . >
</UserControl>
And in the above, the toolkit:DataGrid is set to the namespace for the WPFToolkit. The DataGrid works as designed, I’ve just never done an override within a user control and the code I have above never fires – a breakpoint there is never reached.
Any thoughts?
You have override the method correctly. In case you want to override the
OnCreateAutomationPeerof yourdataGrid, you have to subclass theDataGrid–And in xaml, use your custom dataGrid
And in constructor of your UserControl –
You need to ask for
AutomationPeerto hit the breakpoint. Isn’t it how you desired it?This is what you missing –
UIElementAutomationPeer.CreatePeerForElement(dataGrid);