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