0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

WPF でテンプレートの親を取得する

Posted at

環境

VisualStudio 2022
WPF

テンプレートの親を取得する

WPF でテンプレートなどを使う時、たまに VisualTree が途切れてしまう時がある。その時、途切れた要素に ContentPresenter が使われている時には、TemplateParent プロパティを使って、親要素を取得できることがある。

private void RoutedEvent(object sender, RoutedEventArgs args)
{
    var border = args.OriginalSource as Border;
    if(border == null)
    {
        return;
    }
    var cp = border.Child as ContentPresenter;
    if(cp == null)
    {
        return;
    }
    Debug.WriteLine("cp:" + cp.TemplateParent.ToString());
}

としてやると、テンプレートの親要素の型を取得できる。
DataGrid に横付けの DataGrid を更に表示させて、DataGrid でない要素を選択した時に消せなくて困ったため、メモ。

参考

ContentPresenter

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?