3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

TreeViewItemで`ItemContainerGenerator.ContainerFromItem`が取得できない場合

Last updated at Posted at 2014-07-25

ユースケース

XAMLのコンストラクタレベルで、TreeViewにアクセスしてExpandやSelectをしたい場合。

問題

TreeView.ItemContainerGenerator.ContainerFromItemでContainerから情報を解決しようとしても、常にnullが返ってきてしまい、処理できない。

対応策

BeginInvokeを使って処理を後ろに回せばいいようです。

TreeViewItem.Dispatcher.BeginInvoke(() =>
{
	TreeViewItem item = TreeView.ItemContainerGenerator.ContainerFromItem(TreeView.Items[0]) as TreeViewItem;
	// TreeViewの操作...
});

おまけ

そういえば、Flexでも同じような事を苦肉の策でしたような。

参考

3
2
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
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?