开发者

Dynamic menu items in WPF

Is there a way to create a section on a menu for a list of menu items to be populate开发者_高级运维d by something like an ObservableCollection?

I'd like to replicate the Window functionality in Visual Studio, where the open document tabs are listed in a numbered list, limited to the first 10.


See "Merge ContextMenus" from here. You can find more info by searching for CompositeCollection and menu/ContextMenu.

EDIT: Just saw CompositeCollection was already mentioned in a comment, but I'm going to leave this here for reference.


How does your menu get data right now? Is it databound? Check this article for binding your menu with a collection.

Now it is up to you to add logics when to add item to the collection.

For eg: In your scenario, you have to store the open documents in a list. Then you have to filter out the first 10 documents and add it to the children property of the MenuItem class specified in the article.


You can create menu items in code and manually add them to the menu when the form loads. It isn't elegant, but it gets the job done.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜