Rohan Prabhu
Rohan Prabhu

Reputation: 7302

OnApplyTemplate being called inifinitely when in a DataTemplate

I have a custom Windows Phone 7 control called 'TranscriptCell'. I have added a few DependencyPropetys to it, which works perfectly fine. I have added a breakpoint in the void override OnApplyTemplate() method, and when in my XAML file, I use the control like this:

<EUILib:TranscriptCell Title="Hello World" HostO="MSI India" />

the breakpoint is triggered exactly once, i.e. the method void OnApplyTemplate() is called exactly once.

However, when I do something like:

<ListBox x:Name="TranscriptList" ItemsSource="{Binding TranscriptItems}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <EUILib:TranscriptCell
                Title="{Binding ActTitle}"
                HostO="{Binding Host}"
            />
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

the breakpoint is being triggered endlessly. The binded source serves a list of EXACTLY 2 items when I'm testing, and it is confirmed that it is EXACTLY 2. Still, the OnApplyTemplate() method is being called endlessly. Why is this happening?

EDIT: After following from the breakpoint step-by-step, it seems that since there are 2 items that are initialized for each of the item in the list, OnApplyTemplate() is being called again and again for each of the instance. i.e., the methods are being called like, instance1::OnApplyTemplate(), then instance2::OnApplyTemplate(), then instnace1::OnApplyTemplate()... and so on and so forth endlessly.

Upvotes: 0

Views: 301

Answers (1)

Matt Lacey
Matt Lacey

Reputation: 65586

Remove the breakpoint and add required debug output via System.Diagnostics.Debug.WriteLine().
Coming out of the breakpoint is likely triggering the call to UpdateLayout. (I've seen this happen a bunch of times in other scenarios.)

As a quick test, does it still do this when you don't break into that method?

Upvotes: 2

Related Questions