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

假装没事ソ 提交于 2019-12-12 01:43:48

问题


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

回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/2534339/why-is-the-selecttemplate-method-run-2-times-in-debug-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!