WPF和C#:为用户控件中的数据网格创建覆盖?

Unk*_*der 3 c# wpf user-controls datagrid

我有一个大的WPF应用程序,我在用户控件中有一个datagrid,我需要为OnCreateAutomationPeer创建一个覆盖.我在做这件事时遇到了麻烦,事件似乎永远不会发生.在我的代码隐藏中,我有类似的东西

public partial class DocChecklistView :  UserControl, IDataModuleView {     

        protected override System.Windows.Automation.Peers.AutomationPeer OnCreateAutomationPeer()
        {                
            return null;
        }

        public CDocumentChecklistView() {
            InitializeComponent();
        }
}
Run Code Online (Sandbox Code Playgroud)

XAML非常标准,代码类似

<UserControl>
 <Grid>
        <toolkit:DataGrid  ItemsSource="{Binding Source={StaticResource DocumentsVS}}" AutoGenerateColumns="False" CanUserAddRows="False" CanUserDeleteRows="False"
                FontSize="16" Name="_dgDocuments" Style="{StaticResource EklektosDataGridStyle}" . . . >
</UserControl>
Run Code Online (Sandbox Code Playgroud)

在上面,将toolkit:DataGrid设置为WPFToolkit的命名空间.DataGrid设计的作品,我从来没有在用户控件中完成覆盖,上面的代码永远不会触发 - 从未到达断点.

有什么想法吗?

Roh*_*ats 7

您已正确覆盖该方法.如果你想覆盖OnCreateAutomationPeer你的dataGrid,你必须继承DataGrid-

public class MyDataGrid : DataGrid
{
    protected override AutomationPeer OnCreateAutomationPeer()
    {
        return null;
    }
}
Run Code Online (Sandbox Code Playgroud)

在xaml中,使用自定义dataGrid

<local:MyDataGrid x:Name="dataGrid"/>
Run Code Online (Sandbox Code Playgroud)

在UserControl的构造函数中 -

public CDocumentChecklistView()
{
    InitializeComponent();
    AutomationPeer a = UIElementAutomationPeer.CreatePeerForElement(dataGrid);
}
Run Code Online (Sandbox Code Playgroud)

你需要要求AutomationPeer击中断点.不是你想要的吗?

这就是你所遗漏的 - UIElementAutomationPeer.CreatePeerForElement(dataGrid);