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

Posted by msfanboy on Stack Overflow See other posts from Stack Overflow or by msfanboy
Published on 2010-03-28T20:04:50Z Indexed on 2010/03/29 2:13 UTC
Read the original article Hit count: 466

Hello,

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

© Stack Overflow or respective owner

Related posts about datatemplate

Related posts about wpf