get value from radiobutton

sevensakurasevensakura Member ✭✭
edited February 12 in Xamarin.Forms

good day everyone, i wanna ask a simple yet confusing me.
i want to get value from one button on radio button group to make certain listview hide
heres my radiobutton code :

var statusSelection = new RadioButtonGroupView

        { 
            Children = {
                new RadioButton {Text = STATUS.Pending},
                new RadioButton {Text = STATUS.Save, IsChecked = true },
                new RadioButton {Text = STATUS.Disc}

        };

am i do this right or have another way ?
thanks you

Best Answer

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    Register the selected changed event, then you can get the selected index there:

    var statusSelection = new RadioButtonGroupView
    
    {
        Children = {
            new RadioButton { Text = STATUS.Pending },
            new RadioButton { Text = STATUS.Save, IsChecked = true },
            new RadioButton { Text = STATUS.Disc }
    
        }
    };
    
    statusSelection.SelectedItemChanged += StatusSelection_SelectedItemChanged;
    
    private void StatusSelection_SelectedItemChanged(object sender, EventArgs e)
    {
        RadioButtonGroupView radioButtonGroupView = sender as RadioButtonGroupView;
    
        var index = radioButtonGroupView.SelectedIndex;
    }
    
  • sevensakurasevensakura Member ✭✭

    i am sorry i dont get what you mean @LandLu

    private void StatusSelection_SelectedItemChanged(object sender, EventArgs e)
    {
    RadioButtonGroupView radioButtonGroupView = sender as RadioButtonGroupView;

    var index = radioButtonGroupView.SelectedIndex;
    

    }

    am i suppose to insert command here?
    all i want is to make this listview hide when save radiobutton selected

    ListView lvBackground = new ListView
    {
    ItemsSource = data.Background
    };

    thank you

  • LandLuLandLu Member, Xamarin Team Xamurai

    After registering the SelectedItemChanged event, when users select RadioButton the event will be triggered.
    Then you can get the selected index in that event. When you found it is 1 in your case, hide the list view.
    Moreover, I think you need to make your list view as a content page's property so that you can retrieve the list view in that event:

    private void StatusSelection_SelectedItemChanged(object sender, EventArgs e)
    {
        RadioButtonGroupView radioButtonGroupView = sender as RadioButtonGroupView;
    
        var index = radioButtonGroupView.SelectedIndex;
        if (index == 1)
        {
            lvBackground.IsVisible = false;
        }
    }
    
  • sevensakurasevensakura Member ✭✭

    thank you, i get it now thanks to your description, but i am having problem cause lvBackground didn't recognized there

  • sevensakurasevensakura Member ✭✭

    thank you it work with a little edited. God Bless you

Sign In or Register to comment.