Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,
I'm looking for a solution to dynamically load user controls based on the radio button selection. The problem is explained below.
1. I have a base window xaml (MainWindow). This has 2 Stack Panels, SP1 and SP2.
2. I have created 5 User Controls


2.1 Radio UC ==> 4 radio buttons.
2.2 Option-1 UC
2.3 Option-2 UC
2.4 Option-3 UC
2.5 Option-4 UC

3. The Radio UC is loaded in SP1 of the MainWindow in the design time.
4. Based on the selection of the Radio button, one of the 4 Option UC should be loaded dynamically in SP2

I'm clueless as I dont know WPF that well. Could anyone help me with this using WPF MVVM pattern.

Thanks in advance.
Posted

1 solution

Hi TechBang, Please look at this example (But this is not in MVVM pattern, for the MVVM patter follow the link which I gave at last)

First of all as per you requirement I create a design page.
XML
<grid showgridlines="True">
        
        <grid.columndefinitions>
            <columndefinition width="200" />
            <columndefinition width="400" />
        </grid.columndefinitions>
        
        
        <stackpanel name="SP1" grid.column="0">
            <radiobutton groupname="ControlLoad" name="btncontrol"> Button Control                
            </radiobutton>
            <radiobutton groupname="ControlLoad" name="checkcontrol">CheckBox Control
            </radiobutton>
            <radiobutton groupname="ControlLoad" name="textcontrol">TextBox Control
            </radiobutton>
            <radiobutton groupname="ControlLoad" name="dropcontrol">Dropdown Control
            </radiobutton>
            
            <Button Name="btncreate" Content="Create" Margin="10" Click="btncreate_Click"></Button>
        </stackpanel>
        
        <stackpanel name="SP2" grid.column="1">
            
        </stackpanel>
    </grid>


And second part is code behind code.

C#
private void btncreate_Click(object sender, RoutedEventArgs e)
        {
            if (btncontrol.IsChecked.Value)
            {
                Button button = new Button();
                button.Content = "New Button";
                button.Margin = new Thickness(5);
                SP2.Children.Add(button);
            }
            else if (checkcontrol.IsChecked.Value)
            {
                CheckBox check = new CheckBox();
                check.Content = "New CheckBox";
                check.Margin = new Thickness(5);
                SP2.Children.Add(check);
            }
            else if (textcontrol.IsChecked.Value)
            {
                TextBox textbox = new TextBox();
                textbox.Text = "New TextBox";
                textbox.Margin = new Thickness(5);
                SP2.Children.Add(textbox);
            }
            else if (dropcontrol.IsChecked.Value)
            {
                ComboBox combo = new ComboBox();
                combo.Items.Add("Item1");
                combo.Items.Add("Item2");
                combo.Margin = new Thickness(5);
                SP2.Children.Add(combo);
            }
        }


Link for MVVM Pattern is
http://stackoverflow.com/questions/4420911/get-selected-radio-button-in-a-group-wpf[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900