【问题标题】:Updating Listview itemsource clears radiobutton control inside it in Xamarin forms更新 Listview itemsource 在 Xamarin 表单中清除其中的单选按钮控件
【发布时间】:2021-11-16 11:40:13
【问题描述】:

我在 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 时,所有单选按钮都丢失了。 任何帮助将不胜感激。

【问题讨论】:

  • “所有单选按钮都丢失”是什么意思?
  • Listview 不呈现任何单选按钮

标签: android listview xamarin


【解决方案1】:

当您使用 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);
    }
}

截图如下:

【讨论】:

    猜你喜欢
    • 2021-12-02
    • 1970-01-01
    • 2012-03-28
    • 2021-06-26
    • 1970-01-01
    • 2016-08-14
    • 1970-01-01
    • 2018-04-11
    • 1970-01-01
    相关资源
    最近更新 更多