views:

264

answers:

3

Is there a way to create a section on a menu for a list of menu items to be populated 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.

A: 

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.

Veer
Hmm, really kinda sucks that I have to put my static menu items in with the non-static items. I'll accept this as the correct answer if I don't get any pure WPF solutions. I really feel that, since MS uses the feature in their own IDE that they would have built it more into WPF, at least with a couple of converters or something.
mattdekrey
@mattdekrey: What is a pure WPF solution? why this is not a pure WPF solution?
Veer
It's WPF + C# - I'm hoping to do it all with databindings and without any additional custom code. I'd really like to keep my presentation layer declarative (the point of WPF) to reduce the logic in my backing classes.
mattdekrey
There is a CompositeCollection (http://msdn.microsoft.com/en-us/library/system.windows.data.compositecollection.aspx) that can be used to merge in static items with an observable collection inline in the XAML - a pure XAML solution. (Apparently I confused "WPF" and "XAML" in previous comments.)
mattdekrey
A: 

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.

Jonathan Allen
A: 

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.

Djof