Sort hidden radiobuttons in WPF stackpanel

Asked

Viewed 42 times

-3

I have a stackpanel with several radiobuttons on it, depending on the user input some of these radiobuttons are hidden (visibility property = visibility.Hidden). I would like as this happens, the stackpanel (or other component) organizes the radiobuttons so that the visible ones go up the list and the hidden ones go down. What happens nowadays is that I hide the radiobuttons, but even hidden they remain at the top of the list, making the visible ones stay below.

    For Each item As RadioButton In spItens.Children
        If Not item.Content.ToString.Contains(txtBusca.Text) Then
            item.Visibility = Visibility.Hidden
        ElseIf txtBusca.Text = "" Then
            item.Visibility = Visibility.Visible
        End If
    Next

1 answer

0

Good morning Fernando, you will have to create an Array with the position of each radiobutton

Dim Posicoes As Point() = {RadioButton1.Margin, RadioButton2.Margin, RadioButton3.Margin} 
' Precisam estar em ordem

Then in your function you would have to call the Array in Order, as follows

Dim n As Integer
        For Each item As RadioButton In spItens.Children
            If Not item.Content.ToString.Contains(txtBusca.Text) Then
                item.Visibility = Visibility.Hidden
            ElseIf txtBusca.Text = "" Then
                item.Visibility = Visibility.Visible
                item.Margin= Posicoes(n) ' Mover o item para a posição
                n += 1 ' Pular para a segunda posição e assim por diante
            End If
        Next

That is, the value of "n" will only be incremented when it is a command that is to be visible...

  • Thanks for your help, but I couldn’t find the 'Location' property on radiobutton.

  • So @Fernandofragoso, this is kind of weird because I don’t recognize this property "Visibility", just "Visible", but anyway,when you enter the properties of the control, don’t have something like "Location"? A field containing the values "X" and "Y"?

  • No. Remembering that I am doing in WPF, which differs somewhat from VB.NET windows Forms.

  • @Fernandofragoso I got it, I looked on the internet here and the property "Location" in WPF is the same as "Margin", I made the change in the original answer, give a look to see if now it will work...

  • @Fernandofragoso It worked?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.