【问题标题】:WPF & C#: Create an override for a datagrid within a user control?WPF 和 C#:为用户控件中的数据网格创建覆盖?
【发布时间】:2012-10-22 15:49:13
【问题描述】:

我有一个大型 WPF 应用程序,其中我在用户控件中有一个数据网格,我需要为 OnCreateAutomationPeer 创建一个覆盖。我在这样做时遇到了麻烦,而且该事件似乎永远不会触发。在我的代码隐藏中,我有类似的东西

public partial class DocChecklistView :  UserControl, IDataModuleView {     

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

        public CDocumentChecklistView() {
            InitializeComponent();
        }
}

XAML 的代码非常标准

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

在上面,toolkit:DataGrid 设置为 WPFToolkit 的命名空间。 DataGrid 按设计工作,我从未在用户控件中进行过覆盖,并且我上面的代码从未触发 - 从未到达断点。

有什么想法吗?

【问题讨论】:

  • 顺便说一句,我正在尝试解决这个问题中提出的问题:stackoverflow.com/questions/4017786/…
  • 那么,您面临什么问题?只是那个断点没有命中?
  • 没错 - 我认为我没有正确实现覆盖

标签: c# wpf datagrid user-controls


【解决方案1】:

您已正确覆盖该方法。如果你想覆盖 dataGridOnCreateAutomationPeer,你必须继承 DataGrid-

public class MyDataGrid : DataGrid
{
    protected override AutomationPeer OnCreateAutomationPeer()
    {
        return null;
    }
}

在 xaml 中,使用您的自定义 dataGrid

<local:MyDataGrid x:Name="dataGrid"/>

在你的 UserControl 的构造函数中 -

public CDocumentChecklistView()
{
    InitializeComponent();
    AutomationPeer a = UIElementAutomationPeer.CreatePeerForElement(dataGrid);
}

您需要请求AutomationPeer 才能命中断点。不是你想要的吗?

这就是你所缺少的 - UIElementAutomationPeer.CreatePeerForElement(dataGrid);

【讨论】:

    【解决方案2】:

    您需要在自定义的DataGrid 控件中重写OnCreateAutomationPeer 方法,如下所示:

    public partial class MyDataGrid : DataGrid
    {
        protected override System.Windows.Automation.Peers.AutomationPeer OnCreateAutomationPeer()
        {
            return null;
        }
    }
    

    然后将它添加到您的 UserControl 中,就像这样(在构建项目后使用 Visual Studio 拖放):

    <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>
    

    您指定需要覆盖 OnCreateAutomationPeer 方法。仅当您的应用程序使用 UIA(UI 自动化),并且如果应用程序的该部分受 UIA 约束(据我所知)时,才需要这样做。如果没有,那么您甚至不需要重写 OnCreateAutomationPeer 方法。但是,如果您的应用程序的所有功能都仅限于 UIA,那么您需要确保您还为您的自定义控件实现了以下五个步骤(请阅读this 文档以获取更多信息):

    1. 通过重写 OnCreateAutomationPeer 向 UIA 公开控件
    2. 通过覆盖核心方法提供正确的属性值
    3. 使客户端能够使用方法与控件交互
    4. 实施模式提供者
    5. 引发事件

    如果您想获取控件的自动化对等对象(您可以使用此对象来获取有关控件特性和功能的信息并模拟交互使用),您可以在使用 FromElement- 创建它后获取它-方法,如果没有,您可以使用 CreatePeerForElement 方法。

    AutomationPeer automationPeer = UIElementAutomationPeer.CreatePeerForElement(checklistView.myDataGrid1);
                automationPeer = UIElementAutomationPeer.FromElement(checklistView.myDataGrid1);
    

    无论您使用何种 UIA 方法(测试代码或构建提供辅助功能或其他功能的应用程序),您都需要确保已为自定义控件制定了支持 UIA 的要求,并且已创建 AutomationPeer 对象,以便自动化代码可以使用它来获取有关控件特性和功能的信息或模拟交互使用。

    【讨论】:

      【解决方案3】:

      Override 似乎是正确的,您只需创建一个 AutomationPeer 即可获得要命中的断点:

      XAML:

      <local:DocChecklistView x:Name="DocChecklistView" Initialized="DocChecklistView_Initialized"/>
      

      在 CodeBehind 到上面的 XAML 中:

      private void DocChecklistView_Initialized(object sender, EventArgs e)
      {
         var peer = UIElementAutomationPeer.CreatePeerForElement(DocChecklistView);
      }
      

      如果您想在 UserControl 中为 toolkit:DataGrid 覆盖 OnCreateAutomationPeer,您必须将 toolkit:DataGrid 子类化为您在 UserControl 中使用的 CustomControl

      【讨论】:

        猜你喜欢
        • 2016-06-14
        • 1970-01-01
        • 2010-12-12
        • 2011-12-26
        • 1970-01-01
        • 1970-01-01
        • 2010-10-31
        • 2012-04-23
        • 2014-03-28
        相关资源
        最近更新 更多