You need to override the OnCreateAutomationPeer
method in a self defined DataGrid
control, like so:
public partial class MyDataGrid : DataGrid
{
protected override System.Windows.Automation.Peers.AutomationPeer OnCreateAutomationPeer()
{
return null;
}
}
Then add it to your UserControl, like so (just drag and drop using Visual Studio after building the project):
<UserControl xmlns:YourApplicationNamespace="clr-namespace:YourApplicationNamespace" x:Class="YourApplicationNamespace.DocChecklistView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid Margin="0,1,0,-1">
<YourApplicationNamespace:MyDataGrid x:Name="myDataGrid1" />
</Grid>
</UserControl>
You are specifying that you need to override the OnCreateAutomationPeer-method. This is only to be done if your application is making use of UIA (UI Automation), and also, if that part of the application is subject to UIA (from what I can understand). If not, then you don't even need to override the OnCreateAutomationPeer-method. However, if your application is restricted to UIA in all of its features then you need to make sure you've also implemented the following five steps (please read this documentation for more information on this) for your Custom Control:
- Expose the control to UIA by overriding OnCreateAutomationPeer
- Provide correct property values by overriding Core methods
- Enable the client to interact with the control using methods
- Implement Pattern providers
- Raise events
If you want to get the automation peer object of a control (you can use this object to get information about a control’s characteristics and features and to simulate interactive use) you can either get it once it has been created using the FromElement-method and if not, you can use the CreatePeerForElement-method.
AutomationPeer automationPeer = UIElementAutomationPeer.CreatePeerForElement(checklistView.myDataGrid1);
automationPeer = UIElementAutomationPeer.FromElement(checklistView.myDataGrid1);
Whatever UIA approach (testing code or building applications that provide accessibility features or other) your using, you need to make sure that the requirements have been made for the Custom Control to support UIA ánd that the AutomationPeer-object has been created so the Automation code can use it to get information about the control’s characteristics and features or to simulate interactive use.