Cui Pengfei 崔鹏飞
Cui Pengfei 崔鹏飞

Reputation: 8305

OnApplyTemplate method does not get called when the custom control is being rendered

I have made a custom control for windows phone 7.

If I use this control in XAML code, it shows up in both the designer and the emulator.

But if I create in C# code and render it in a WriteableBitmap, nothing shows up in the WriteableBitmap, and the OnApplyTemplate does not get called either.

I tried calling the ApplyTemplate method and the Measure and Arrange methods, but none of them can make the control to be rendered.

So what is the right approach to make a custom control renderable in a WriteableBitmap?

Thanks.

Upvotes: 3

Views: 3080

Answers (1)

Ed Bayiates
Ed Bayiates

Reputation: 11230

Before a WPF template is applied, which includes Silverlight, an object must be added to a visual tree. Calling ApplyTemplate will simply fail if the object isn't in a visual tree. So to make it render properly, add it to a visual tree, then you can call ApplyTemplate / UpdateLayout / etc. and have it render correctly.

Upvotes: 3

Related Questions