【发布时间】:2015-02-25 08:42:25
【问题描述】:
当您add 一个 VSTO(非 Word 原生)content control 时,您指定名称:
controls.AddContentControl(wordRange, "foo", wdType);
其中 controls 是 VSTO(扩展)Document.Controls 集合。
您可以稍后通过名称look up 控件:
ContentControl myContentControl = controls["foo"];
那么为什么在世界上没有 ContentControl 的 Name 属性呢? (或 ContentControlBase,或任何其他衍生产品)。
我正在为 Document.Controls 属性实现一个包装类,它允许您添加或迭代内容控件。在迭代底层 Document.Controls 时,无法查找每个控件的名称。 (我们需要它来返回 ContentControl 包装器的实例)。所以目前我在我们的 ContentControls 包装类中这样做:
public IEnumerator<IContentControl> GetEnumerator()
{
System.Collections.IEnumerator en = this.wordControls.GetEnumerator();
while (en.MoveNext())
{
// VSTO Document.Controls includes all managed controls, not just
// VSTO ContentControls; return only those.
if (en.Current is Microsoft.Office.Tools.Word.ContentControl)
{
// The control's name isn't stored with the control, only when it was added,
// so use a placeholder name for the wrapper.
yield return new ContentControl("Unknown", (Microsoft.Office.Tools.Word.ContentControl)en.Current);
}
}
}
我宁愿不必在我们的 ContentControls 对象中保留名称到包装器对象的映射。谁能告诉我如何获取控件的名称(传递给 Controls.Add() 的名称参数?
【问题讨论】:
标签: c# .net ms-word ms-office vsto