Click here to Skip to main content
15,878,430 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have a wpf form where I am adding a user control when the user checks a certain radio control. If the control is unchecked then I need for the user control to be removed.

Here is what I was trying to do:
Private Sub CtlRadioStacker1_rdYesChecked(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles CtlRadioStacker1.rdYesChecked
    Dim cbMaterial As New ctlComboBoxStacker
    If Me.CtlRadioStacker1.rdYes.IsChecked = True Then
        cbMaterial.Name = "cbMaterial"
        cbMaterial.Label.Content = "Material"
        Dim location As Integer = StackPanelMain.FindName("cbMaterial")
    End If
End Sub

This code adds the control just fine, however it does not remove the control as I had expected. Not surprisingly if I toggle the radio buttons it adds additional copies of the control while never removing the last, I am aware of this and will correct it once I solve this dilemma.

Any help would be greatly appreciated.


You should remove it in the Unchecked event instead I think.
Here's two solutions, depending on what criteria for identifying the control to remove:

Private Sub RadioButton_Checked(sender As Object, e As RoutedEventArgs)

    Dim button As Button = New Button With {.Content = "Some content", .Name = "MyName"}

End Sub

Private Sub RadioButton_UnChecked(sender As Object, e As RoutedEventArgs)

    ' Remove by control type, repeat if there can be many of the same type
    'theStackpanel.Children.Remove(theStackpanel.Children.Cast(Of UIElement).Where(Function(element) element.GetType() = GetType(Button)).First())

    ' Remove by control name
    theStackpanel.Children.Remove(theStackpanel.Children.Cast(Of FrameworkElement).Where(Function(element) element.Name = "MyName").Single())

End Sub

Hope this helps,
Fredrik Bornander
Share this answer
Tarun.K.S 3-May-11 9:35am    
Are you sure it works? I tried that before, it didn't work for me. Also you need another radiobutton to "uncheck" it.
Fredrik Bornander 3-May-11 9:45am    
Works fine for me.
Tarun.K.S 3-May-11 10:18am    
Ohkay cool.
JasonSelf 3-May-11 12:54pm    
Thanks, that worked great.
Tarun.K.S 3-May-11 14:30pm    
Gets my 5 too.
Here is how did. Assuming the UserControl to be added is defined in the Resources of Grid.

<grid name="grid">
    <local:ausercontrol x:key="myUserControl" xmlns:x="#unknown" xmlns:local="#unknown" />
<RadioButton Margin="28,29,0,0" Name="radioButton1" Height="16" HorizontalAlignment="Left" VerticalAlignment="Top" Width="120">RadioButton</RadioButton>
<StackPanel Margin="28,81,26,28" Name="stackPanel1"/>
<RadioButton Height="16" HorizontalAlignment="Right" Margin="0,29,38,0" Name="radioButton2" VerticalAlignment="Top" Width="120">RadioButton</RadioButton>

And in my code-behind, I did this :

public class Window1
UserControl uc;
    public Window1()
        radioButton1.Checked += new RoutedEventHandler(radioButton1_Checked);
        radioButton1.Unchecked += new RoutedEventHandler(radioButton1_Unchecked);
    void radioButton1_Unchecked(object sender, RoutedEventArgs e)
    void radioButton1_Checked(object sender, RoutedEventArgs e)
        uc = (UserControl)grid.FindResource("myUserControl");

I have added another radiobutton(radiobutton2) so that the unchecked event can be handled for radiobutton1.
Did this work for you?
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