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

2

0

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;
    }
}
Mousse answered 28/3, 2010 at 20:4 Comment(0)
P
0

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.

Phanerozoic answered 29/3, 2010 at 2:14 Comment(0)
C
0

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.

Cuzco answered 29/3, 2010 at 2:49 Comment(1)
Hello MIke, do you have any info/links about your source about the empty/loading etc? I havent found anything.Mousse

© 2022 - 2024 — McMap. All rights reserved.