calling method in my View

Feb 17, 2012 at 9: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.

Coordinator
Feb 17, 2012 at 10: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 5: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?

Coordinator
Feb 18, 2012 at 2: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;
        viewModel.NewCustomers();

        // 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.

Cheers,
Tony

Feb 18, 2012 at 7: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!

Coordinator
Feb 18, 2012 at 7:55 PM
You might want to check this out :-) http://blog.tonysneed.com/2011/04/23/simple-mvvm-toolkit-versus-mvvm-light-toolkit
Feb 18, 2012 at 9:05 PM

In my favourites already ;-)

Feb 18, 2012 at 10:25 PM
Edited Feb 18, 2012 at 10: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:

 ViewModel:

///

<summary>

 

///

Gets the WelcomeTitle property.

 

///

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

 

///

</summary>

 

public string

WelcomeTitle

{

 

get

{

 

return

_welcomeTitle;

}

 

set

{

 

if (_welcomeTitle == value

)

{

 

return

;

}

_welcomeTitle =

value

;

RaisePropertyChanged(WelcomeTitlePropertyName);

}

}

 

//View

 

How do i catch that raisePropertyChanged?

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

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

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