locked
Bubbling event from Model to ViewModel RRS feed

  • Question

  • Hi I have an async call executing in my model which is one dll. How do I get it to bubble up to my viewmodel so it shows in my GUI.

    This is my async function in my model - in my model I also have a property. When the LoadCompleted fires I set the property which fires the Notify property event. How do I get this event to "bubble up" to my view model so it will know the property has changed?

    private void GetBySupplierID_LoadCompleted(object

    sender,

     

    LoadCompletedEventArgs

    e)

    {

     

    if (e.Error == null

    )

    {

     

    var lst = new DataServiceCollection<Supplier_Addresses_Grouped

    >();

    Supplier_Addresses_List = (

    DataServiceCollection<Supplier_Addresses_Grouped

    >)sender;

    }

     

     

     

    }

    Tuesday, April 12, 2011 11:29 AM

Answers

  • Hi,

    Could you add more description about your scenarios? So that we can give you alternative chioce.

    Your view model or model class may represent a collection of items, or it may define one or more properties that return a collection of items. In either case, it is likely that you will want to display the collection in an ItemsControl, such as a ListBox, or in a DataGrid control in the view. These controls can be data bound to a view model that represents a collection or to a property that returns a collection via the ItemSource property.

     

    To properly support change notification requests, the view model or model class, if it represents a collection, should implement the INotifyCollectionChanged interface (in addition to the INotifyPropertyChanged interface). If the view model or model class defines a property that returns a reference to a collection, the collection class returned should implement the INotifyCollectionChanged interface.

    However, implementing the INotifyCollectionChanged interface can be challenging because it has to provide notifications when items are added, removed, or changed within the collection. Instead of directly implementing the interface, it is often easier to use or derive from a collection class that already implements it. The ObservableCollection<T> class provides an implementation of this interface and is commonly used as either a base class or to implement properties that represent a collection of items.

    If you need to provide a collection to the view for data binding, and you do not need to track the user's selection or to support filtering, sorting, or grouping of the items in the collection, you can simply define a property on your view model that returns a reference to the ObservableCollection<T> instance.

    Thursday, April 21, 2011 9:09 AM

All replies

  • what does your Supplier_Addresses_List property setter look like?

    Wednesday, April 13, 2011 6:24 PM
  • Hi,

    I recommand you set the property value in LoadCompleted method but store the property in ViewModel and add RaisePropertyChanged event in the setter of that property.

    Then RaisePropertyChanged event will be fired in ViewModel when the property is changed in Model.

     

    Best Regards,

    Tuesday, April 19, 2011 1:27 AM
  • I'm a bit confused. Currently I am using MVVM Light Messenging to pass the object back to the ViewModel and change the event there. Is this a bad way of doing it. I'm a bit unclear how you set the ViewModel property from the Model? 

    Tuesday, April 19, 2011 7:34 AM
  • Hi,

    Could you add more description about your scenarios? So that we can give you alternative chioce.

    Your view model or model class may represent a collection of items, or it may define one or more properties that return a collection of items. In either case, it is likely that you will want to display the collection in an ItemsControl, such as a ListBox, or in a DataGrid control in the view. These controls can be data bound to a view model that represents a collection or to a property that returns a collection via the ItemSource property.

     

    To properly support change notification requests, the view model or model class, if it represents a collection, should implement the INotifyCollectionChanged interface (in addition to the INotifyPropertyChanged interface). If the view model or model class defines a property that returns a reference to a collection, the collection class returned should implement the INotifyCollectionChanged interface.

    However, implementing the INotifyCollectionChanged interface can be challenging because it has to provide notifications when items are added, removed, or changed within the collection. Instead of directly implementing the interface, it is often easier to use or derive from a collection class that already implements it. The ObservableCollection<T> class provides an implementation of this interface and is commonly used as either a base class or to implement properties that represent a collection of items.

    If you need to provide a collection to the view for data binding, and you do not need to track the user's selection or to support filtering, sorting, or grouping of the items in the collection, you can simply define a property on your view model that returns a reference to the ObservableCollection<T> instance.

    Thursday, April 21, 2011 9:09 AM