King Chan
King Chan

Reputation: 4302

Binding collection of objects to Canvas in MVVM?

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 :)

Upvotes: 1

Views: 2000

Answers (2)

Josh C.
Josh C.

Reputation: 4363

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.

Upvotes: 1

brunnerh
brunnerh

Reputation: 184376

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.

Upvotes: 1

Related Questions