c# - VSTO/WinWord 绑定(bind)到 DropDownListContentControl 的进入事件

标签 c# vsto contentcontrol

我有一个 VSTO addin(不是模板项目),我想为手动添加到文档的任何内容控件绑定(bind)到 Entering 事件,但不知道如何进行绑定(bind)。

很容易找到合适的内容控件:

ActiveDoc.ContentControls.Cast<ContentControl>()
    .Where(cc => cc.Type == WdContentControlType.wdContentControlDropdownList)

ContentControl 根本没有任何事件,所以我似乎需要以某种方式获得 DropDownListContentControl 但我一直无法弄清楚如何从 ContentControl 或任何其他来源获取。

  • 您不能只将 ContentControl 转换为 DropDownListContentControl,因为没有共同的父级。
  • 我以为我可以使用动态 找到,但那些不支持事件。
  • 模板项目中,ContentControl 是使用 Globals.Factory.CreateDropDownListContentControl 创建的,但这似乎不适用于插件项目。

我猜我遗漏了一些简单的东西,但到目前为止,谷歌搜索数小时都没有结果。

TIA

最佳答案

我想我只是看的时间不够长。为此,您需要将 Microsoft.Office.Interop.Word.Document 包装在一个容易混淆的 Microsoft.Office.Tools.Word.Document 中,使用以下命令:

Microsoft.Office.Tools.Word.Document doc 
    = Globals.Factory.GetVstoObject(Addins.Globals.Application.ActiveDocument);

该类具有一组方法,这些方法将生成一个包装器,其中包含比 native 控件更多的事件。因此,要在任何下拉列表中获取一个事件,您可以执行以下操作:

        Microsoft.Office.Tools.Word.Document doc = Globals.Factory.GetVstoObject(ActiveDoc);
        foreach (ContentControl cc in ActiveDoc.ContentControls) {
            if (cc.Type == WdContentControlType.wdContentControlDropdownList) {
                var dropList = doc.Controls.AddDropDownListContentControl("MyControl");
                dropList.Tag = "MyControl";
                dropList.Entering += (sender, args) => {
                    var that = (DropDownListContentControl) sender;
                    Debug.Print("Entering: " + that.Tag);
                };
            }
        }

仅此而已,除了控件本身从可用性角度来看非常糟糕之外。

参见 Host Items and Host Controls Overview

关于c# - VSTO/WinWord 绑定(bind)到 DropDownListContentControl 的进入事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36896184/

相关文章:

c# - 在 ASP.NET 中设置 SameSite=None 和 Secure

c# - 从 Excel.Interop 表中删除和插入多行

wpf - ContentControl 崩溃中的绑定(bind)

wpf - 无法通过 DataTrigger 设置 ContentTemplate

c# - 使用 OpenXML 读取 Excel 文件

c# - Caliburn micro 在使用 mef 时找不到 MainView

c# - 方法推断不适用于方法组

c# - 如何将 Excel Interop 与 ClosedXml 混合使用 : styles

vba - 如何使用 VSTO 在 PowerPoint 文本框中设置左边距