Excel 2003 ActionsPane中的AutoSize ElementHost

时间:2009-05-21 22:13:51

标签: .net wpf winforms excel vsto

我在Excel 2003 ActionsPane中托管WPF图表。图表设置为水平和垂直拉伸,但是,虽然ElementHost和图表水平填充ActionsPane,但我还没有找到一种方法来使ElementHost垂直填充。唯一似乎对ElementHost布局有影响的属性是Height和Size属性。 Anchor,Dock,AutoSize似乎不会影响ActionsPane对象或ElementHost对象的布局。

我错过了什么吗?

的问候,

丹尼

// A snippet from ThisWorkbook.cs
public partial class ThisWorkbook
{
    private void ThisWorkbook_Startup(object sender, System.EventArgs e)
    {

        var ap = Globals.ThisWorkbook.ActionsPane;
        ap.Clear();
        ap.Visible = true;
        var plotControl1 = new Swordfish.WPF.Charts.TestPage();
        var elementHost1 = new System.Windows.Forms.Integration.ElementHost();
        elementHost1.AutoSize = true; // Doesn't seem to have an effect.
        elementHost1.Child = plotControl1;

        ap.Controls.Add(elementHost1);

    }

1 个答案:

答案 0 :(得分:3)

创建一个名为my ActionPane的自定义WPF表单,并将其托管在ElementHost中。以下是我如何使用ElementHost本身:

private void ThisDocument_Startup(object sender, System.EventArgs e)
    {
        ActionPane actionPaneControl = new ActionPane();
        this.ActionsPane.Resize += new EventHandler(ActionsPane_Resize);
        this.ActionsPane.Controls.Add(new ElementHost { Child = actionPaneControl, AutoSize = true });
    }

基本上我订阅了ActionsPane Resize事件,并根据它调整了ElementHost对象的大小。这为WPF控件(包括vert和horiz拉伸)提供了附加的附带好处,与Office应用程序窗口一起调整大小

void ActionsPane_Resize(object sender, EventArgs e)
    {
        ((this.ActionsPane.Controls[0] as ElementHost).Child as ActionPane).Height = this.ActionsPane.Height;
    }