为什么 SelectTemplate 方法在调试模式下运行 2 次?
调试这个类时SelectTemplate方法运行了2次,但是为什么呢?
第一次该项目始终为空。
public class PersonDataTemplateSelector : DataTemplateSelector
{
public override DataTemplate SelectTemplate(object item,DependencyObject container)
{
if (item is Person)
{
Person person = item as Person;
Window window = Application.Current.MainWindow;
if (System.ComponentModel.DesignerProperties.GetIsInDesignMode( window))
return null;
if (person.Gender == "male")
return window.FindResource("boysViewTemplate") as DataTemplate;
else
return window.FindResource("girlsViewTemplate") as DataTemplate;
}
return null;
}
}
debuging this class the SelectTemplate Method is run 2 times, but why?
The first time the item is always null.
public class PersonDataTemplateSelector : DataTemplateSelector
{
public override DataTemplate SelectTemplate(object item,DependencyObject container)
{
if (item is Person)
{
Person person = item as Person;
Window window = Application.Current.MainWindow;
if (System.ComponentModel.DesignerProperties.GetIsInDesignMode( window))
return null;
if (person.Gender == "male")
return window.FindResource("boysViewTemplate") as DataTemplate;
else
return window.FindResource("girlsViewTemplate") as DataTemplate;
}
return null;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以设置断点并检查堆栈跟踪进行验证,但我相信在设置可视化树时会使用空输入调用它一次,而第二次是在实际填充绑定时。
You can set a break point and check the stack trace to verify but I believe it's called once with a null input when the visual tree is set up and the second time is when the bindings are actually populated.
如果您的选择器要提供“空”或“正在加载”的外观,则第一次调用将使您的选择器有机会在加载元素时提供该模板。
If your selector were to provide a look for "Empty" or "Loading", the first call is what gives your selector the opportunity to provide that template while the elements are loading.