0

Is there something like an "OnPaint" method in Silverlight?

Back when I was writing C++, I found that it was easy to use the OnPaint event to customize the display of a class to the screen?

Is there an equivalent in Silverlight? If I want to do something when a UserControl is displayed on the screen, what method would I override?

I noticed this post: C# WPF OnPaint method alternative? but it seems that in Silverlight, thre is no "OnRender" method for a UserControl class.

Community
  • 1
  • 1
xarzu
  • 8,657
  • 40
  • 108
  • 160
  • 1
    The OnRender doesn't exist in Silverlight. This is one of the reasons I can't convert some of my code to it. – Ed Bayiates Aug 01 '11 at 18:12
  • Are you aware that the presence of such a method has nothing to do with the programming language you're using? If there were an OnPaint method you could access from C#, then you could also use it from VB.NET or any other .NET language. – John Saunders Aug 01 '11 at 18:13
  • 2
    Is there something *specific* that you're trying to accomplish? – Gabe Aug 01 '11 at 18:19
  • I'd recommend using the built-in drawing and styling functionality of silverlight rather than trying to apply old-school methods to a newer framework. Custom controls are done much differently now. – Merlyn Morgan-Graham Aug 01 '11 at 18:57
  • The event that works is LayoutUpdated – xarzu Aug 02 '11 at 00:02

3 Answers3

2

OnPaint was a workaround... to allow you to customise the appearance of controls. That was because you did not have much control over the default appearance of any controls in WinForms applications.

With Silverlight that all changes. Every control is now effectively skinned, using templates and styles, and there are few limitations on how you can customise them. There are far too many links so I just grabbed a couple for you.

Get yourself a good book on Silverlight and learn the proper way to work with it (not around it). This one is one of my favorites.

If you have specific things you are trying to do, to the appearance of user controls, best to list those instead and find out the best way to do it the Silverlight way. :)

iCollect.it Ltd
  • 92,391
  • 25
  • 181
  • 202
  • +1 but I would word it that __Using__ OnPaint to __customise__ the appearance of an __existing__ control is a hack. OnPaint in general is the correct place to be drawing the UI for a new original discrete control. – AnthonyWJones Aug 01 '11 at 20:49
  • @AnthonyWJones: Just poetic licence... removed :) – iCollect.it Ltd Aug 01 '11 at 21:11
0

You haven't specified what you're trying to do. If you just want to know when a frame is being rendered, the CompositionTarget.Rendering Event will tell you that. If you actually want to draw on the frame being rendered, you cannot do so.

Gabe
  • 84,912
  • 12
  • 139
  • 238
0

It is LayoutUpdated. As in:

...
            this.LayoutUpdated += new EventHandler(LayoutUpdated);

}

  void LayoutUpdated(object sender, EventArgs e)
{}
xarzu
  • 8,657
  • 40
  • 108
  • 160