我在 Excel 2003 ActionsPane 中托管 WPF 图表。图表设置为水平和垂直拉伸(stretch),但是,虽然 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);
}
最佳答案
创建一个名为 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 控件(垂直和水平拉伸(stretch))随 Office 应用程序窗口调整大小的额外好处
void ActionsPane_Resize(object sender, EventArgs e)
{
((this.ActionsPane.Controls[0] as ElementHost).Child as ActionPane).Height = this.ActionsPane.Height;
}
关于.net - Excel 2003 ActionsPane 中的 AutoSize ElementHost,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/895507/