View getting notified from VM

Topics: Framework Services
Dec 22, 2011 at 12:48 PM


I am using CM in my WP7 project. I have a problem which I have found the solution using EventAggregator but want to know from the experts that is this the correct way or is there any better way of approaching the issue ?

So here is the problem

I have view which has StackPanel and the button below it.

I want to add a textbox under this stackpanel calling StackPanel.Children.Add() method when the button is clicked

Currently my view has subscription of this event and the VM publishes this event.

Is this approach correct or is there a better way of doing it in MVVM pattern rather then pub/sub approach.




Dec 22, 2011 at 2:22 PM

Hi maqinfo

Are you trying to 'add items to the stackpanel' ?

If so then you can bind the ItemsSource property  of the stackpanel to a BindableCollection<TItemType> Property on your view model ...

then set the Button's name to 'AddItem' 

in you viewmodel have a public method called AddItem which does just that .. e.g. adds an item to the BindableCollection<TItemType> Property 

This will automagically add the item to the stackpanel


Hopefully that's helpful (that's if I've understood what you're trying to do ;-) )