以下は、すべての親の名前を として取得する拡張メソッドですIEnumerable<string>
。
public static class Extensions
{
public static IEnumerable<string> GetControlPath(this Control c)
{
yield return c.Name;
if (c.Parent != null)
{
Control parent = c.Parent;
while (parent != null)
{
yield return parent.Name;
parent = parent.Parent;
}
}
}
}
そして、これを利用するプロジェクトに追加した UserControl のプロパティを次に示します。
public partial class CustomControl : UserControl
{
public CustomControl()
{
InitializeComponent();
}
public string ControlPath
{
get
{
return string.Join(@"\", this.GetControlPath().Reverse());
}
}
}
ビルド後、ユーザー コントロールをツールボックスからフォームにドラッグします。他のコントロール内にネストしてください。3 つのパネルをネストし、例のように最も内側のパネルに配置しました。設計時のプロパティは次のようになります。

これは、 から派生する作成するすべてのクラスに適用できますControl
。IExtenderProvider
ここでは無関係のようです。