I have a program I'm taking from Code Behind and converting to MVVM. The program is very simple. It takes text from text boxes and converts the strings into a pipe delaminated text file. My program has a MainViewWindow and 3 User Controls that use a ModernUI tab system to determine which User Control populates the MainViewWindow. I'm only working on the first User Control for now. I'm using a SimpleIoc (ViewModelLocator) to identify which User Control is selected and "active" on the MainViewWindow. The User Control has ~30 text boxes that are bound to the ViewModel. Some of these text boxes have buttons that (are supposed to) activate a pop up window that asks for additional information. For example, the 'Name:' text box has a button next to it that should pop up a window that asks for the First Name, Last Name, Middle Name, Suffix and Prefix all in separate text boxes. This information is parsed into a single string and separated via '^' symbol and placed back into the 'Name:' text box. That won't be hard but I want to provide the meat and potatoes.
My problem is I have about 8 places where I need a button to pop up the 'additional information' window. All of these have different information needed so I have created User Controls with the required fields. I'm trying to dynamically add to this pop up window when the button is pushed. I don't know how to pop up a window and add a User Control to a Window via commanding essentially. This is my logic based off research and conformity with the MVVM framework. If I am on the right path (which I sure hope!) can someone fill in the gaps? If there is a better way, point me in the right direction please?
From what I understand, you will need to create a new form and then call it from the button.
Once this form comes up, you dynamically add controls to it that are appropriate for that button.
here is button code to show the additional form:
I won't provide the whole code for you but to dynamically create a control and actually use it, you will need the following info
Once the form loads up, it should call some method that will build the controls. This question has been answered before here: How do I create 5 buttons and assign individual click events dynamically?
I personally don't like WPF's default PopupControl for a number of reasons, so have created my own which I think goes better with the MVVM pattern
Even if you create your own, the basic idea is the same. You place your custom PopupControl on top of your View in a control that allows objects to overlap, such as a Grid, like this
And you hook up a few bindings on the popup control
Then from your ViewModel you just set the appropriate properties to have it appear
Then magic occurs causing the Popup to appear overtop of your main view, with it's content property set to your new ViewModel, drawn using a DataTemplate of your definition, and all is good and right in the world once again.
But in all seriousness, here's the code I use for my custom PopupControl, in case my blog link ever stops working. I'd still recommend you read it for an example though.