VSTO:仅在加载文档时启用功能区按钮
如何设置 Word 加载项中的功能区按钮在加载文档时启用,在未加载文档时禁用,就像大多数内置按钮一样?
可以将全局标志绑定到按钮的“Enabled”属性,还是比这更复杂?我知道我可以创建一个定时循环来检查 Application.Documents 中的更改,但如果可能的话,我正在寻找“更干净”的东西。
我已经看过 禁用自己的 Word 2007 Add-在“如果没有加载文档”和其他相关问题中。
How does one set a ribbon button in a Word add-in to be enabled when a document is loaded and disabled when no documents are loaded, just Like most of the built-in buttons?
Can one bind a global flag to the "Enabled" property of the button, or is it more complicated than that? I know I could create a timed loop that checks for changes in Application.Documents, but I'm looking for something "cleaner" if possible.
I've already looked at Disable Own Word 2007 Add-In If No Document Loaded and other related questions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有几种方法可以处理这个问题。
首先,您可以创建一个公开的函数,该函数为按钮的启用状态返回 true 或 false(但是您想要确定),然后定义功能区 xml 以指向 Enabled 属性 getter 的该函数。如果您正在处理基于 IExtensibility 的插件,那么这就是您必须采用的方法。
如果您正在处理 VSTO,请在功能区设计器中定义功能区按钮并默认将其设置为禁用。
然后,在 STARTUP 事件期间,挂钩 WORD 对象,特别是 NEWDOCUMENT、DOCUMENTOPEN 和 WINDOWACTIVATE 事件。
在每个事件的事件处理程序代码中,根据触发的事件和当时激活的文档,启用或禁用适用的按钮。
There are several ways to handle this.
first, you can create a publicly exposed function that returns true or false for the enabled state of your button (however you want to determine that), you then define your ribbon xml to point to that function for the Enabled property getter. If you're dealing with an IExtensibility based addin, then this is the way you'd have to go.
If you're dealing with VSTO, then define your ribbon button in the ribbon designer and make it DISABLED by default.
Then, during the STARTUP event, hook the WORD object, specifically the NEWDOCUMENT, DOCUMENTOPEN and WINDOWACTIVATE events.
In the event handler code for each of those events, enable or disable your buttons as applicable depending on which event fired and which document was activated at the time.
请改用 DocumentChange 事件。连接将是这样的:
处理程序
Use the DocumentChange event instead. Hook up will be something like this:
And the Handler
有趣的是,我的 VSTO Contrib 项目 (http://vstocontrib.codeplex.com/documentation) 有一些功能,使功能区管理更简单。
最干净的方法是使用我的功能区工厂,但是如果没有视图模型来查询按钮的状态,则需要更新项目以禁用按钮。事实上,这是我还没有真正涉及到的场景。
加载项感兴趣的有 3 个部分:视图(窗口)、上下文(文档)和功能区。 VSTO Contrib 意味着您可以获得每个上下文的视图模型,并且它管理/抽象功能区和视图,因此看起来每个上下文都有一个功能区,并且它告诉您当前的活动视图(对于显示相同文档场景的多个窗口)。
缺少的部分是,如果有功能区,但没有上下文,也没有视图模型,它应该使该功能区控件无效并禁用它。这应该是一个非常简单的更改,如果您有兴趣试用 VSTO Contrib 的 RibbonFactory,请给我发电子邮件,我可以为您进行此更改。
Interesting, my VSTO Contrib project (http://vstocontrib.codeplex.com/documentation) has some features which make ribbon management simpler.
The cleanest way is to use my ribbon factory, but the project will need to be updated to disable buttons if there are no viewmodels to query for the status of the button. In fact it is a scenario I havent really covered.
You have 3 parts an add-in is interested in, the view (window), the context (the document) and the ribbon. VSTO Contrib means you get a view model per context, and it manages/abstracts the ribbon and view so it appears you have a ribbon per context, and it tells you the current active view (for multiple windows showing same document scenarios).
The missing part is if there is a ribbon, but no contexts and no viewmodels, it should invalidate that ribbon control and disable it. It should be a pretty simple change, email me if you are interested in giving VSTO Contrib's RibbonFactory a spin and I can make this change for you.