简体   繁体   English

为什么SelectTemplate方法在调试模式下运行2次?

[英]Why is the SelectTemplate Method run 2 times in debug mode?

debuging this class the SelectTemplate Method is run 2 times, but why? 调试此类,SelectTemplate方法将运行2次,但是为什么呢?

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;
    }
}

您可以设置一个断点并检查堆栈跟踪以进行验证,但是我相信在设置可视化树时调用一次,输入为空,而第二次是在实际填充绑定时调用。

如果选择器要提供“空”或“加载”外观,则第一个调用是使选择器有机会在加载元素时提供该模板的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM