Wednesday, November 28, 2007

WPF quiz #1: Moving rectangles

Referring last event, I want to start new section in my blog, named "WPF quiz".  In this section, I'd like to ask you WPF related question and a day later answer the question my self. So, let's start

Question:
I have to display large amount of rectangles, which moving randomly at the screen. What approach, should I use in order to gain best performance for my application?

Answers (creation):

  1. Create DependencyObject with relevant properties. Create ObservableCollection of those objects. Create DataTemplate to visualize the object into Rect. Bind ObservableCollection to Children property of Canvas.
  2. Create UIElement, that draws Rect. Create ObservableCollection of those UIElements. Bind ObservableCollection to Children property of Canvas.
  3. Create FrameworkElement hosts Rect. Create ObservableCollection of those FrameworkElements. Bind ObservableCollection to Children property of Canvas.
  4. Create CustomControl, that visualizes Rect. Create ObservableCollection of those FrameworkElements. Bind ObservableCollection to Children property of Canvas.

Answers (movement):

  1. Bind attached properties Canvas.Left, Canvas.Top to X and Y of the objects
  2. Bind RenderTransform -> TranslateTransform property of the objects to X and Y
  3. Create and remove appropriate objects with preset X and Y in ObservableCollection
  4. Create DispatcherTimer to change X and Y properties of the objects.

You should choose one from Answers(creation) section and one from Answers(movement) section to archive full answer to this question.

Please use comments for this post to answer.

My answer will come tomorrow. Good luck.

No comments: