首页 > 解决方案 > 更新 Listview itemsource 在 Xamarin 表单中清除其中的单选按钮控件

问题描述

我在 Xamarin 表单中有一个简单的列表视图。定义如下。

<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>

在后面的代码中,我将列表视图绑定如下

            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;

现在,当我更新 ItemsSource 时,所有单选按钮都丢失了。任何帮助将不胜感激。

标签: androidlistviewxamarin

解决方案


当你使用 lvRadio.ItemsSource = null;lvRadio.ItemsSource = temp;

会导致listview的所有值都被修改,所以会出现问题。

有两种解决方案:

  1. 将 ListView 修改为ObservableCollection

    删除lvRadio.ItemsSource= null;lvRadio.ItemsSource = temp;

    这样每次修改temp的值,都会自动填充界面,不会修改原来的值。

  2. RadioButton 有一个IsChecked属性来记录 RadioButton 是否被选中,所以可以在 CommonModel 中添加一个属性来记录 IsChecked 是否被选中。然后使用 IsChecked="{Binding xxx}"

这是第一个解决方案的cs页面代码:

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);
    }
}

这是屏幕截图:

在此处输入图像描述


推荐阅读