如何获取 Ribbon 控件类上的自定义任务窗格对象

发布于 2024-12-12 12:45:37 字数 84 浏览 0 评论 0原文

开发 Excel vsto 项目,我如何处理功能区控件类中的自定义任务窗格。 例如,我想在单击功能区控件的按钮时显示自定义任务窗格。

朵拉

Developing a Excel vsto project, how could I handle the Custom Task Pane in the Class which is a Ribbon Control.
For example, I would like to show the Custom Task Pane when I click the button of the Ribbon Control.

Dora

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

鯉魚旗 2024-12-19 12:45:38

我假设您正在使用 Excel VSTO 加载项和功能区可视化设计器。您可以通过加载项上的属性访问自定义任务窗格来实现您想要的目的:

public partial class ThisAddIn
{
   private CustomTaskPane taskPane; 
   internal CustomTaskPane TaskPane
   {
      get
      {
         return this.taskPane;
      }
   }

...并在功能区中添加按钮,并为单击事件添加事件处理程序,通过全局访问加载项:

private void MyRibbonButton_Click(object sender, RibbonControlEventArgs e)
{
   Globals.ThisAddIn.TaskPane.Visible = true;
}

我不久前写了一篇文章 描述了过程,你可能会发现它很有用。
使用 xml 功能区这也是可行的。

I assume you are working with an Excel VSTO add-in, with the Ribbon Visual Designer. You can achieve what you want by making your custom Task Pane accessible via a property on your Add-In:

public partial class ThisAddIn
{
   private CustomTaskPane taskPane; 
   internal CustomTaskPane TaskPane
   {
      get
      {
         return this.taskPane;
      }
   }

... and adding a button in your Ribbon, and adding an event handler for the click event, accessing the add-in via Globals:

private void MyRibbonButton_Click(object sender, RibbonControlEventArgs e)
{
   Globals.ThisAddIn.TaskPane.Visible = true;
}

I wrote a post a while back which describes the process, you may find it useful.
This is also feasible using the xml ribbon.

戈亓 2024-12-19 12:45:38

这可以通过拥有 Win Forms 用户控件来完成。
我曾参与过一个项目,其中我们必须扩展 MS Word 并需要此功能,但相同的示例也适用于 Excel。

我在网上偶然发现的另一个有趣的方法是拥有一个 Windows 用户控件并在 Windows 控件中托管一个 WPF 用户控件!
这当然可以让您利用 WPF 提供的所有出色工具,下面是一个示例:

1)在功能区上放置一个切换按钮(可视化设计器)。这将用于显示隐藏任务窗格。
使用 ToggleButton 是一个不错的选择,因为按下时它会突出显示。

2) 将以下代码添加到 ToggleButton 的单击事件

 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)用户控件(名称 - taskPaneControl1)

5.2)用户控件(WPF),(名称 - con)

使用我使用过的名称会有所帮助复制/粘贴下面的代码,但如果您愿意,可以通过任何方式更改它

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

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
AddTaskPane();
taskPaneValue.Visible = false;
}

当打开 MS Office 应用程序时,任务窗格将被隐藏,切换可见属性以在启动事件中更改此设置。
导航到 ToggleButton 并按几次以确保任务窗格按预期显示

另请查看以下链接 我的大部分代码都来自这里 - http://xamlcoder.com/cs/blogs/joe/archive/2007/07/17/using-wpf-with-vsto-office-2007.aspx

This can be accomplished by having a Win Forms user control.
I've worked on a project where we had to extend MS Word and needed this functionality, but the same example will apply to Excel.

Another interesting way I stumbled upon on the net is to have a Windows user control and host a WPF user control within the Windows control!
This ofcourse allows you to take advantage of all the awesome tools you get with WPF, here is an example:

1)Drop a ToggleButton on a Ribbon(Visual Designer).This will be used to show hide the task pane.
Using ToggleButton is a good choice as it appears highlighted when pressed down.

2)Add below code to the click event of the ToggleButton

 Globals.ThisAddIn.TaskPane.Visible = ((RibbonToggleButton)sender).Checked;

3)Add a reference from your project to the following assembly - WindowsFormsIntegration

4)In your ThisAddIn.cs add the two using directives listed below:

   using Microsoft.Office.Tools;
   using System.Windows.Forms.Integration;

5)Add two user controls

5.1)User control (name - taskPaneControl1)

5.2)User control(WPF),(name - con)

Using the names I've used will help when copying/pasting the code below but by any means change it if you wish to

6)Add below code to the ThisAddIn.cs class

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)Add the two code below to the Startup event in your ThisAddIn.cs

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
AddTaskPane();
taskPaneValue.Visible = false;
}

When an MS Office Application is opened the task pane will be hidden toggle the Visible property to change this in the Startup event.
Navigate to the ToggleButton and press it a few times to make sure the task pane is showing as expected

Also have a look at the following link most of my code came from here - http://xamlcoder.com/cs/blogs/joe/archive/2007/07/17/using-wpf-with-vsto-office-2007.aspx

萌化 2024-12-19 12:45:38

这是一个艰巨的挑战,因为功能区和任务窗格是独立的实体。主要挑战之一是每个检查器只有一个 Ribbon 类实例和多个任务窗格实例。为此,需要对 Office 内部结构有一定的深入了解。

该解决方案还取决于您使用的是功能区 XML 还是功能区设计器。您使用哪种方法?

This is a difficult challenge since the Ribbon and Task Panes are separate entities. One of the main challenges is that there is only one instance of the Ribbon class and multiple instances of the task pane for each inspector. To this properly requires some advanced understanding of the Office internals.

The solution also depends on whether you are using the Ribbon XML or Ribbon Designer. Which approach are you using?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文