views:

83

answers:

1

Hello,

I have a list of CustomerViewModels in a ComboBox. The selected CustomerViewModel I want to delete and also the Customer wrapped inside it to remove it from the repository.

But how can I access the Customer model inside the CustomerViewModel?

+2  A: 

Just a suggestion, make your collection of customerviewmodels an ObserableCollection of CustomerViewModels. what this buys you is a CollectionChanged Event that you could listen on with a delegate for changes to the collection ie deletion, so from there you could manipulate you model accordingly

http://msdn.microsoft.com/en-us/library/ms653375(VS.85).aspx

perhaps something like

public class CustomersViewModel: ViewModelBase
{
    public ObservableCollection<CustomersViewModel> Customers { get; private set; }

    public CustomersViewModel()
    {
        Customers = new ObservableCollection<CustomersViewModel>(GetCustomers());
        Customers.CollectionChanged += 
            (sender, args) =>
                {
                    if (args.Action ==  NotifyCollectionChangedAction.Remove)
                    {
                        foreach (CustomerViewModel customerViewModel in args.NewItems)
                        {
                            DeleteCustomer(customerViewModel.Customer);
                        }
                    }
                };
    }

    private void DeleteCustomer(Customer customer)
    {
        // Call into your repo and delete the customer.
    }

    private List<CustomersViewModel> GetCustomers()
    { 
        // Call into your model and return customers.
    }

    ... ICommands ect... 

}
almog.ori
already use ObservableCollection etc... All here is setup fine just did not see that my customer object in the CustomerViewModel class has a "_" char at the beginning of the instance name so I did not see it when I typed "SchoolclassViewModel." ;-)I put this in the CustomerViewModel: public Schoolclass SelectedSchoolclass { get { return _schoolclass;} }to get the customer...thx again without that post I maybe wouldnt have seen my typo ;P
msfanboy
:) anytime, here to help
almog.ori