开发Excel vsto项目,我如何处理作为功能区控件的类中的自定义任务窗格。 例如,我想在单击功能区控件的按钮时显示自定义任务窗格。
多拉
答案 0 :(得分:4)
我假设您正在使用功能区可视设计器使用Excel VSTO加载项。您可以通过加载项上的属性访问自定义任务窗格来实现您的目的:
public partial class ThisAddIn
{
private CustomTaskPane taskPane;
internal CustomTaskPane TaskPane
{
get
{
return this.taskPane;
}
}
...并在功能区中添加一个按钮,并为click事件添加事件处理程序,通过Globals访问该加载项:
private void MyRibbonButton_Click(object sender, RibbonControlEventArgs e)
{
Globals.ThisAddIn.TaskPane.Visible = true;
}
我写了一篇帖子describes the process,您可能会觉得它很有用。 使用xml功能区也是可行的。
答案 1 :(得分:3)
这可以通过使用Win Forms用户控件来实现。 我参与了一个项目,我们不得不扩展MS Word并需要这个功能,但同样的例子将适用于Excel。
我在网上偶然发现的另一个有趣的方法是拥有Windows用户控件并在Windows控件中托管WPF用户控件! 这个课程允许您利用WPF获得的所有优秀工具,这是一个例子:
1)在功能区上删除ToggleButton(可视设计器)。这将用于显示隐藏任务窗格。 使用ToggleButton是一个不错的选择,因为它在按下时会突出显示。
2)将以下代码添加到ToggleButton
的click事件中 Globals.ThisAddIn.TaskPane.Visible = ((RibbonToggleButton)sender).Checked;
3)将项目中的引用添加到以下程序集--WindowsFormsIntegration
4)在你的 ThisAddIn.cs 中添加下面列出的两个using指令:
using Microsoft.Office.Tools;
using System.Windows.Forms.Integration;
5)添加两个用户控件
5.1)用户控件(name - taskPaneControl1)
5.2)用户控制(WPF),(名称 - 骗局)
在复制/粘贴下面的代码时,使用我使用过的名称会有所帮助,但如果您愿意,可以通过任何方式更改
6)将以下代码添加到 ThisAddIn.cs 类
public CustomTaskPane TaskPane
{
get{return taskPaneValue;}
}
private TaskPaneControl taskPaneControl1;
private CustomTaskPane taskPaneValue;
private WpfControl con;
internal void AddTaskPane()
{
ElementHost host = new ElementHost();
con = new WpfControl();
host.Child = con;
host.Dock = DockStyle.Fill;
taskPaneControl1 = new TaskPaneControl();
taskPaneControl1.Controls.Add(host);
taskPaneValue = this.CustomTaskPanes.Add(taskPaneControl1, "My Taskpane");
taskPaneValue.Visible = true;
}
6)将以下两个代码添加到 ThisAddIn.cs
中的Startup事件中private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
AddTaskPane();
taskPaneValue.Visible = false;
}
当打开MS Office应用程序时,将隐藏任务窗格以切换Visible属性以在Startup事件中更改此属性。 导航到ToggleButton并按几次以确保任务窗格按预期显示
另请查看我的大部分代码来自以下链接 - http://xamlcoder.com/cs/blogs/joe/archive/2007/07/17/using-wpf-with-vsto-office-2007.aspx
答案 2 :(得分:0)
这是一项艰巨的挑战,因为功能区和任务窗格是独立的实体。其中一个主要挑战是每个检查器只有一个Ribbon类实例和任务窗格的多个实例。为此,需要对Office内部进行一些深入的了解。
解决方案还取决于您使用的是Ribbon XML还是Ribbon Designer。你使用哪种方法?