Messaging a object comes to late.

Dec 26, 2010 at 2:52 PM

Hi all,

 

I'm using Silverlight 4 with the GalaSoft.MvvmLight Framework.

I'm having a datagrid where the currentitem is bind to a selectedCustomer (CustomerViewModel). When the selectedCustomer change I send it to a selectedCustomer in a ucCustomerContactViewModel. The usercontrol is in a dataform that show the details of the selectedCustomer.

When the app starts and i select a customer the usercontrol doesnt show the selectedCustomer but when I select a second Customer, it show it... So I think that my message with the selectedCustomer is comming too late. Does anyone know a workaround for this problem?

 

Thank you!

<view:ucCustomerContacts Width="400"
                                                         Height="300"
                                                         DataContext="{Binding UcCustomerContactsViewModel, Source={StaticResource Locator}}" />

public ucCustomerContactsViewModel()
        {
            if (IsInDesignMode)
            {
                // Code runs in Blend --> create design time data.
            }
            else
            {
                Messenger.Default.Register(this, new Action<PropertyChangedMessage<Customer>>(UpdateCustomer));

            }
        }
    public void UpdateCustomer(PropertyChangedMessage<Customer> e)
    {
        SelectedCustomer = e.NewValue as Customer;
    }

Coordinator
Jan 13, 2011 at 5:41 PM

Hi,

Sorry for the delay.

This scenario should work, if you make sure that the ucCustomerContactsViewModel is instantiated before the message is sent. I find that creating the critical VMs in advance in the ViewModelLocator is the best way to resolve this kind of issues.

Alternatively, you need to implement a "fetch" mechanism when ucCustomerContactsViewModel is created, in order to get the correct values on startup.

Makes sense?

Cheers,

Laurent