开发者

Binding ContextMenu on flattened ViewModel

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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜