WPF quiz #1: Moving rectangles

28 בנובמבר 2007

תגיות: , , ,
4 תגובות

[This blog was migrated. You will not be able to comment here.
The new URL of this post is http://khason.net/blog/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.

הוסף תגובה
facebook linkedin twitter email

כתיבת תגובה

האימייל לא יוצג באתר. שדות החובה מסומנים *

4 תגובות

  1. Andres28 בנובמבר 2007 ב 17:34

    Answers 2 for creation and 4 movement.

    הגב
  2. art vandaly28 בנובמבר 2007 ב 19:44

    i can't understand why not use System.Windows.Shapes.Rectangle class to render the rectangle, looks like the overhead of framework element and shape is very low and frees you from implementing all the properties/rendering logic.
    unless your talking about a really really big collection of object in that case there should be a general rectangle container that renders all the rectangles by itself. I'm not sure if that what you meant in 3. that would mean

    הגב
  3. Eran Kampf28 בנובמבר 2007 ב 19:59

    I'm for #2 (creation) and #4 (movement) too…

    הגב
  4. Tamir Khason28 בנובמבר 2007 ב 22:04

    Vandalay, you are right. This is one of options.
    However, let's say, that I need custom properties (blinking) foe example.

    הגב