1

I kind of want to make a game.... so I want able to dynamically to add a data object to the Canvas and is dispaly it.

i.e) 1) a Car object, contains the car information, when it binds to the Canvas, I want to display it as a UserControl (contains some graphic).

2) When the car object display display/add/remove to the Canvas, it should do some animation.

So there is 3 questions:

  1. Is it possible to binds to Canvas directly to do things I describe above?
  2. Is it better I just do it in programmatic way in the code-behind?
  3. How do such thing in MVVM? If my Car collection objects is in my ViewModel, the only way I can do is binds that to the View? Then if I want to let the car do some animation when user press some key, how can I interact with the View using my ViewModel?

Some suggestion will be appreciated :)

King Chan
  • 4,212
  • 10
  • 46
  • 78

2 Answers2

1

To display animations when the items are added to a panel you can use Fluid Layout from the Blend SDK. (Some demonstration video; sadly doesn't show the generated code for those without Blend)

For methods to bind to a canvas see this question.

I would not recommend doing this imperatively.

Community
  • 1
  • 1
H.B.
  • 166,899
  • 29
  • 327
  • 400
  • Imperatively? So I should stay using MVVM for this? Is there any reason? – King Chan Jan 10 '12 at 18:57
  • @KingChan: It's not about MVVM, it's about imperative vs. declarative. Data binding and XAML are declarative, setting values and giving instructions via C# is imperative. Imperative is a lot more error prone in my opinion, your second question seemed to ask whether to add the items or animations manually and i would not recommend that. – H.B. Jan 10 '12 at 19:07
1

I assume you want multiple objects on your canvas. To do so you need to template an ItemsControl. Template the panel to be a canvas. This is all done in the presentation layer with XAML. Then bind your ItemsControl's ItemsSource to the collection in your view model.

Josh C.
  • 4,303
  • 5
  • 30
  • 51