views:

36

answers:

1

Hi,

suppose the following classes:

public class Model
{
    public ObservableCollection<A> Items { get; set; }
}

public class A
{
    ObservableCollection<B> Data { get; set; }

    //... some more properties
}

public class B
{
    //..properties
}

The model is bound to a RibbonMenu and should also be used in a context menu. The context menu must be bound to all items of class B in the model. Changes in the model (new items added, items removed, items changed ...) should change both the context menu and the RibbonMenu.

The RibbonMenu works nicely but how is it possible to bind the contextmenu without creating a separate model?

A: 

You could create wrapper properties that flatten your A and B entities as needed for the view controls and expose them publicly from Model.

So, for instance, in Model, you have a private backer of ObservableCollection<A>. Then you have a public ObservableCollection<A> that simply returns the private backer for the ribbon to bind to.

Then also have a public ObservableCollection<B> that does whatever it needs to do in its getter to return what you want for the context menu. For example, if you want the distinct Bs across all As, have the getter do a query on all of A's Bs to return the correct list.

Finally, to tell the view that changes were made in Model, implement INotifyPropertyChanged and raise the PropertyChanged event in the setters of your public members.

MarkB
Thats what I tried but when I add a new item to the collection in class A the context menu doesn´t update - which seems logic because how how should the ObservableCollection<B> in the model class know of this change?
Marcel