How to provide an extendable WPF ContextMenu with MVVM - wpf

I’m building a user control that has a context menu with several items, and I want the users of that control to be able to append items to that context menu.
I thought of adding an attached property where users can specify menu items, but I couldn’t find a way to easily add these items (e.g. in an ItemsControl container) to the control’s ContextMenu in an MVVM way (i.e. using binding and not code-behind).
Do you have an idea how this can be done?

If you have Dictionary<int,MenuItem> and you will have a place that user can select menus. Then add selected menu items to ContextMenu when your control loaded.
I hope this helps.

Related

Custom Items control Approach

I need to build a custom items control in WPF, where the user can drag/drop the items. Normally I would just maintain a list of view models and use a data template to define how the items should be displayed (in this case, a button). But I am concerned that this will make the drag/drop difficult as the ItemsSource objects will be view model objects , not the actual button.
My other potential approach is when an object is added to the ItemsSource, create a button in c# and add it manually, that way I can access the button directly to do drag/drop.
What would your advice be?
I have solved my issue. I found a way to get the control being render from the data template

Expand Usercontrol to full window size

I am currently working on a project which has a tab control which contains a Wrap panel which contain a series of user controls. I am looking for a way to allow the user to select one user control and maximize it to the size of the tab control/window.
One thought is to simply remove all the other items from the panel.However I am attempting to use MVVM as much as possible and I'm not sure how much the user control should know about the panel. (The user control will contain a button to allow maximizing)
Is there a way to temporarily remove the usercontrol from the grid and treat it like a modal popup or just to fill the window?
How about having "Visible" or "Maximized" bool properties in the view model for each user control based item, and databind said user controls Visibility property to the appropriate property. Then bind your user controls maximize/restore button to command in the view model to change the VM properties appropriately?

Add MenuItems to existing ContextMenu within a Style in XAML

I have a style for a control and I'd like to add items to the context menu of that control. I was able to replace the context menu with my own but have found no way to add items to the existing one. Is this possible in XAML?
Thanks!
You can always toggle a MenuItem's visibility in XAML using bindings. This allows you to create one context menu but control the options displayed to the user entirely through XAML. This is what I've done in the past as it seems to be the easiest option.

Dynamically Load UserControl using MVVM Light Toolkit

I searched this site and i found 2 Links
how to load wpf usercontrol in MVVM pattern
MVVM-Light: Load UserControl into Window
but still i cant find the answer to my problem and this link
MVVM-Light Locator Pattern and Reusable UserControl
i Didn't Understood.... so here is i am stating my problem which might help others struggling like me ......
I have a MainWindow which has 2 parts one has a TreeView(a
UserControl) and the other Displays different user controls(named DisplayPanel).... just like windows Explorer.
The Display Panel on the Right side will display different user controls on Clicking nodes of tree view.
and my TreeView is Itself a user Control.
How can i make this composite UI Work using MVVM. Also I am planning to use MVVM light Toolkit. Does this have something that can help...
An Example will be great
Thanks... :)
Edit
My TreeView in a UserControl I made a dependency property in the UserControl which catches the selected Item fo the tree view so that i can use this dependency property to populate the required view in the "MainView" ContentControl binding....as you advised me in the comments. Everything is till now
Problem is that i want to display data contained in the the selected item and i cannot set the DataContext of the UserControls(which will be displayed in right hand side) to the selected item as then i will not be able to use my view model for the respective usercontrol for commands and other operations
I tried to solve this too.... i used the Mediator (Messenger) in my TreeViewUserControl view model to send a Message to the Usercontrol's(the one that i need to display) view model . Message will be passed whenever the item is selected in the tree view. and message contains the selected node. I forgot to mention i set the datacontext of the UserControl to its view model so that i could display data
But using this approach the problem is that when the I click a type of node for the first time the data is not populated but if the same type of node is clicked again its populated. What’s happening is that UserControls object is availabe when the the tree item is clicked for the first time and Mediator sends the message. So Mediator is not able to pass the message to the userControl view model.....
I totally do not have ne idea to solve this further.... is my way if displaying user control right or I should do something that else....totally confused.....
You could try defining a DataTemplate for each type in the TreeView's ItemsSource and instead of having a specific UserControl on the right side, just bind to the TreeView's SelectedItem. Alternatively, you could use a DataTemplateSelector.
Edited for OP's Edit
Did you do this?
MainWindow has TreeView whose ItemsSource=Binding MainVM.Items.
MainWindow has ContentControl whose Content=Binding TreeView.SelectedItem.
Somewhere in project, have ResourceDictionary where each possible type in MainVM.Items has a DataTemplate defined?
Which ViewModel (MainVM or ItemVM) are you trying to use and why can't you use it?

Silverlight TreeView in ComboBox (or just a custom dropdown control)

I was wondering if I can have a ComboxBox with a Treeview as the selector rather than a list?
I note that there is a ComboBox.ItemsPanelTemplate, but I don't know what I need to do to get this to work?
I could build a control 'from the ground-up' with a separate TextBox, Button and TreeView in a PopUp control, but wondered if there is an easier way.
Lee
Are you just wanting the combobox for presentation purposes regarding the treeview?
Why not use a treeview as is and just trigger the display via visibility or animation from a button or control of some sort?
I am working on similar theme as we speak. I am using the Expander control from the Silverlight Toolkit with a TreeView control inside it. When an item is selected in the TreeView, it sets the Header of the Expander and then collapses the Expander.
Initial tests indicate it's OK but as I say, just started with it myself. Will let you know if I hit any stumbling blocks.

Resources