Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>To do what you want to do, you'll want to use a combination of controls and forms. Please note, my answer is applicable to Silverlight 4 and up. First of all, i would bind the button "COMMAND" to a view model command implementation. In the following example, my control's DataContext is an instance of my view model. My ViewModel implements a hash table of a set of Keys(strings) and ViewModelCommand classes. Ignore the complexity of the implementation. The point is the Button is bounded to a "COMMAND"</p> <pre><code>&lt;Button Style="{StaticResource stTitleButton}" Height="Auto" Foreground="White" Command="{Binding ViewModelCommandIoC.ViewModelCommands[FIND]}" CommandParameter="{Binding ElementName=enHiddenControl}" Visibility="{Binding CanSwitchCustomer,Converter={StaticResource VisibilityValueConverter},FallbackValue=Collapsed}" &gt; </code></pre> <p>In the example above, the command is bound to my command indexed under "FIND". It's the same as just binding directly to a command. The next important piece is the CommandParameter. This is a hidden instance of the control that you wish to show in a new window. The name of the control in this case is 'enHiddenControl'. You may place this control anywhere on your screen, just make sure its hidden. This will come in as the parameter in the method signature of the command. For example:</p> <pre><code> /// &lt;summary&gt; /// Finding an existing customer /// &lt;/summary&gt; /// &lt;param name="sender"&gt;The command parameter - control &lt;/param&gt; /// &lt;param name="e"&gt;&lt;/param&gt; public void OnChangeCustomer(object sender)... </code></pre> <p>When this sender comes in, the goal is to get the type and use the activator to create an instance of that class. The Activator is found in <strong>System.Reflection</strong> or <strong>System.Reflection.Emit</strong>. (I'm typing this from the top of my head and it's primarily theoretical, but should work ;) </p> <pre><code>Activator.CreateInstance(sender.GetType()) </code></pre> <p>Once you create an instance of this class, add it to the LayoutGrid on a "ChildControl". How? Create a grid, name it "LayoutRoot". Create a ChildControl, add the grid to the child control, then add the instance that you just created to the grid. Show the form. Theoretically, you've just dynamically created a form with your control without knowing about specific classes. Only generics. The sizing of the form....that's another story. I would use Auto for both width and height. And the size of the control should as a result, theoretically dictate the size of the form.</p> <p>At the end the method should look something like this: </p> <pre><code>public void OnChangeCustomer(object sender) { //the search screen if (sender is UserControl) { UserControl uc = Activator.CreateInstance(sender.GetType()) as UserControl; Grid grdLayoutRoot = new Grid(); grdLayoutRoot.Name = "LayoutRoot"; grdLayoutRoot.Children.Add(uc); ChildWindow cw = new ChildWindow(); cw.Content = grdLayoutRoot; cw.Show(); } } </code></pre> <p>All of this is THEORETICAL and have never been tested, but it should work, given the amount of times I've pulled stunts of these sort of nature in C#.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload