ItemsControl和DataTemplate

时间:2012-10-30 03:24:08

标签: .net wpf

似乎将datatemplate设置为新创建的项目需要一些时间。这与How can I retrieve the DataTemplate (and specific objects) of an item in an ItemsControl?相似,但也没有提供令人满意的答案。

var newIndex = this.Items.Add(new object()); // new "data"
var newContainer = (ContentPresenter) this.ItemContainerGenerator.ContainerFromIndex(newIndex);
var uiElem = newContainer.FindName("txt");

对findname的调用将返回null,因此VisualTreeHelper.GetChildrenCount = 0.如果我给它一些时间,模板被绑定并且一切都很好,但这是不确定的。有没有人知道一个事件或强制模板立即绑定的方法?

由于

1 个答案:

答案 0 :(得分:0)

应用程序可以调用ApplyTemplate()来保证元素的可视树完整。如果代码将检查树中的子元素,则可能需要此保证步骤。

相关问题