calling method in my View

Feb 17, 2012 at 10:08 PM

I have an observable collection in my ViewModel and a View chart that binds to it just fine. My problem is that I need to do something more with the chart whenever the collection changes, and I need to do it the CodeBehind. I reckon that I need some kind of eventhandler in my View that listens to the collection, Im just not sure how to accomplish that. Help greatly appriciated as I have been googling around for this for hours now.

Feb 17, 2012 at 11:46 PM
You should be able to expose the collection as a property on the ViewModel, which is set to the DataContext on the View. From there you can handle any events you like.
Feb 18, 2012 at 6:38 AM

tony if you read my post you should know that I am aware that I can do this. I just do not know how. Could you provide an example?

Feb 18, 2012 at 3:53 PM

That was the best response I could give from my phone. ;)  Seriously, though, what you need to do is grab the ViewModel from the DataContext in the View, then handle the events.  I'm not sure what events you're interested in, but ObservableCollection doesn't expose very many useful events.  Instead I would recommend using a PagedCollectionView, which has a number of events, including CurrentChanged.  Here's how you might do that:

public partial class CustomerView : UserControl
    private CustomersViewModel viewModel;

    public CustomerView()
        // Get customers
        viewModel = (CustomersViewModel)DataContext;

        // Handle CurrentChanged event
        viewModel.Customers.CurrentChanged += OnCustomersCurrentChanged;

    void OnCustomersCurrentChanged(object sender, EventArgs e)
        var customer = viewModel.Customers.CurrentItem as Customer;
        if (customer != null)
            currentCustomerInfo.Text = string.Format("Current Customer Id: {0}", customer.CustomerId);

You can download a working sample here.


Feb 18, 2012 at 8:35 PM

Now that, my friend and savior, was helpfull!! Exactly what I want. Im using Galas mvvm framework though and the project im working on is using observables. Ill see if they got something like it. Thx a bunch for leading me in the right direction!

Feb 18, 2012 at 8:55 PM
You might want to check this out :-)
Feb 18, 2012 at 10:05 PM

In my favourites already ;-)

Feb 18, 2012 at 11:25 PM
Edited Feb 18, 2012 at 11:27 PM

"Right direction" seemed to lead me to your framework :-).... Anyone knows how to do this in mvvm light?


Here is an example from the default project:






Gets the WelcomeTitle property.



Changes to that property's value raise the PropertyChanged event.





public string














if (_welcomeTitle == value







_welcomeTitle =









How do i catch that raisePropertyChanged?

Feb 19, 2012 at 1:15 AM
I think it's pretty much the same regardless of the MVVM toolkit you use.
Feb 19, 2012 at 8:04 AM

Im sure it is..... Has anyone experience in MVVMLigth regarding this, and maybe provide an example?

Feb 19, 2012 at 4:17 PM
You might have better luck posting your question on the Discussion board for that toolkit. ;-)