Home > Software engineering >  Updating Listview itemsource clears radiobutton control inside it in Xamarin forms
Updating Listview itemsource clears radiobutton control inside it in Xamarin forms

Time:09-23

i have a simple listview in Xamarin forms. It is defined as follows.

<ListView x:Name="lvRadio" Grid.Row="1"
           SeparatorVisibility="None"             
           VerticalOptions="CenterAndExpand"
                                      HorizontalOptions="StartAndExpand" HasUnevenRows="True" BackgroundColor="#ffffff"
                                       HeightRequest="300">
                                <ListView.ItemTemplate>
                                    <DataTemplate>
                                        <ViewCell>
                                            <StackLayout BackgroundColor="White">
                                                <RadioButton   BackgroundColor="#ffffff"
                                                                                 Content="{Binding Description}" 
                                                                                 FontFamily="Roboto" FontSize="16" TextColor="Black"
                                                                                 ></RadioButton>
                                            </StackLayout>
                                        </ViewCell>
                                    </DataTemplate>
                                </ListView.ItemTemplate>
                            </ListView>

In the code behind i am binding the listview as follows

            List<CommonModel> temp = new List<CommonModel>();
            CommonModel model;

            model = new CommonModel();
            model.Description = "Radio 1";
            temp.Add(model);

            model = new CommonModel();
            model.Description = "Radio 2";
            temp.Add(model);

            model = new CommonModel();
            model.Description = "Radio 3";
            temp.Add(model);

            lvRadio.ItemsSource = null;
            lvRadio.ItemsSource = temp;

Now when i update the ItemsSource all the radiobuttons are lost. Any help will be appreciated.

CodePudding user response:

When you use lvRadio.ItemsSource = null;lvRadio.ItemsSource = temp;

it will cause all the values of the listview to be modified, so there will be problems.

There are two solutions:

  1. Modify ListView to ObservableCollection,

    delete lvRadio.ItemsSource= null;lvRadio.ItemsSource = temp;

    so that every time the value of temp is modified, the interface will be automatically filled and the original value will not be modified.

  2. RadioButton has an IsChecked property to record whether the RadioButton is selected, so you can add a property to the CommonModel to record whether IsChecked is selected. Then use IsChecked="{Binding xxx}"

Here is the cs page code for the first solution:

public partial class MainPage : ContentPage
{
    ObservableCollection<CommonModel> temp = new ObservableCollection<CommonModel>();
    CommonModel model;
    public MainPage()
    {
        InitializeComponent();
        lvRadio.ItemsSource = temp;
    }
    private void Button_Clicked(object sender, EventArgs e)
    {
        model = new CommonModel();
        model.Description = "Radio 1";
        temp.Add(model);
        model = new CommonModel();
        model.Description = "Radio 2";
        temp.Add(model);
        model = new CommonModel();
        model.Description = "Radio 3";
        temp.Add(model);
    }
}

Here is the screenshot:

enter image description here

  • Related