コンピュータや音楽の事書いてます

Microsoft Office VSTOアドインのCustomTaskPane の幅調整

VSTOアドインでCustomTaskPane にUserControlを埋め込む際の幅の調整方法。
taskPaneの幅に、UserControlが収まらないのを自動調整はしてくれない。
UserControlの幅はTaskPaneに入れると変化してしまう(なぜだ?)ので苦戦した。

色んな値を足したりして試行錯誤した結果:
CustomTaskPanes.Addの手前で覚えておいたwidthを直後で適用すればOK。マージンとすこし余裕を足すとベター。

var usercontroll1 = new UserControl1();
int width = usercontroll1.Width;
var tp = CustomTaskPanes.Add(usercontroll1, "なまえ1");
tp.Visible = true;
tp.Width = width + usercontroll1.Margin.Horizontal + 8;