none
Messaging a object comes to late..

    Question

  • 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!

    Sunday, December 26, 2010 4:55 AM

Answers

  • Greets

    I have seen people use the 'Loaded' event on the View and in the event, make a call on the ViewModel which sets initial properties and notifies the View after everything is ready to show.  Below is an example of how I've done it (note that I keep a reference to the ViewModel in the View and use that in the anonymous delegate in order to ensure if it fires again (such as using View Injection techniques) that it works appropriately:

    public class MyView
    {
       private MyViewModel _myViewModel;

       public MyView()
       {
          _myViewModel = new MyViewModel();

          InitializeComponent();
         
          this.Loaded += (o, e) =>
          {
             _myViewModel.Initialize();
          }
       }
    }

    In the 'Initialize()' member of your ViewModel, set the SelectedCustomer property to a valid property and see if that updates your selection.

    Regards,

    Joe

    Friday, December 31, 2010 10:31 AM

All replies

  • <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;
        }


    Sunday, December 26, 2010 9:26 AM
  • Do I need to add more info or something?

    Monday, December 27, 2010 4:10 PM
  • I don't think the reason is the delay. What is the default value of selectedcustomer? Did you debug and see if the updatecutsom method fires the first time ?

    Monday, December 27, 2010 5:04 PM
  • Greets,

    This is just a guess based on what you've presented so far; however, it seems that your DataContext for your view is being set by a locator which creates an instance of your ViewModel.  However, what you made need to do inside your ViewModel is use a Dispatcher after its creation and then set the selected customer (if anything, perhaps to the first item in your collection).

    Regards,

    Joe

    Tuesday, December 28, 2010 9:23 PM
  • Thank you for your answers! I will look at it on friday and will let you know if it works :)

    Wednesday, December 29, 2010 5:52 AM
  • default value of selectedcustomer is Null. The updateCustomer method does'nt fire the first time.  Here is a printscreen:

    Is it because it's in a tabcontrol?

    Or is there a possibilityfor my UserControlViewModel to ask my CustomerViewModel what his selectedCustomer is on load?

    Wednesday, December 29, 2010 8:27 AM
  • I guess the method message to update customer is not being sent the first time.

    Wednesday, December 29, 2010 4:04 PM
  • and what can I do about it? :)

    Thursday, December 30, 2010 8:13 AM
  • Can you post your code where you are sending the message ?

    Thursday, December 30, 2010 9:33 AM
  •  public Customer SelectedCustomer
            {
                get
                {
                    return _selectedCustomer;
                }
                set
                {
                    if (_selectedCustomer == value)
                    {
                        return;
                    }
                    var oldValue = _selectedCustomer;
                    _selectedCustomer = value;
                    DeleteCommand.RaiseCanExecuteChanged();
                    OverviewCommand.RaiseCanExecuteChanged();
                    RaisePropertyChanged(SelectedCustomerPropertyName, oldValue, value, true);
                    
                    
                }
            }

     public Customer SelectedCustomer

            {

                get

                {

                    return _selectedCustomer;

                }


                set

                {

                    if (_selectedCustomer == value)

                    {

                        return;

                    }


                    var oldValue = _selectedCustomer;

                    _selectedCustomer = value;

                    DeleteCommand.RaiseCanExecuteChanged();

                    OverviewCommand.RaiseCanExecuteChanged();

                    RaisePropertyChanged(SelectedCustomerPropertyName, oldValue, value, true);

                    


                    

                }

            }


    This is in the CustomersViewModel and sets the SelectedCustomer when you select one.

    Friday, December 31, 2010 5:57 AM
  • Greets

    I have seen people use the 'Loaded' event on the View and in the event, make a call on the ViewModel which sets initial properties and notifies the View after everything is ready to show.  Below is an example of how I've done it (note that I keep a reference to the ViewModel in the View and use that in the anonymous delegate in order to ensure if it fires again (such as using View Injection techniques) that it works appropriately:

    public class MyView
    {
       private MyViewModel _myViewModel;

       public MyView()
       {
          _myViewModel = new MyViewModel();

          InitializeComponent();
         
          this.Loaded += (o, e) =>
          {
             _myViewModel.Initialize();
          }
       }
    }

    In the 'Initialize()' member of your ViewModel, set the SelectedCustomer property to a valid property and see if that updates your selection.

    Regards,

    Joe

    Friday, December 31, 2010 10:31 AM