【发布时间】:2021-12-02 03:55:22
【问题描述】:
我已经定义了一个列表视图和 2 个按钮,如下所示。
<ListView x:Name="lvTest" SeparatorVisibility="None" VerticalOptions="CenterAndExpand"
HorizontalOptions="StartAndExpand" HasUnevenRows="True" BackgroundColor="#ffffff">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<RadioButton Padding="10,0,0,0" Grid.Column="0" BackgroundColor="White" TextColor="Black"
GroupName="L1" Content="{Binding Description}"
></RadioButton>
<Label Grid.Column="1" Text="{Binding Amount}" TextColor="Black" BackgroundColor="White"
HorizontalTextAlignment="End" Padding="0,0,10,0"></Label>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<Button x:Name="btnAdd" Clicked="btnAdd_Clicked" Text="Add"/>
<Button x:Name="btnRemove" Clicked="btnRemove_Clicked" Text="Delete"/>
以下是代码隐藏类中的代码
public partial class Listview : ContentPage
{
ObservableCollection<CommonModel> temp = new ObservableCollection<CommonModel>();
CommonModel model;
public Listview()
{
InitializeComponent();
lvTest.ItemsSource = temp;
}
void btnAdd_Clicked(System.Object sender, System.EventArgs e)
{
model = new CommonModel();
model.Description = "Radio 1";
model.Amount = 100;
temp.Add(model);
}
void btnRemove_Clicked(System.Object sender, System.EventArgs e)
{
temp.RemoveAt(0);
}
}
现在,当我单击删除按钮时,列表视图变为空。 到目前为止一切正常。
现在,当我单击添加按钮时,列表视图不显示单选按钮并且只显示数量。以下是截图
现在,当我再次单击“添加”按钮时,我会在第二行看到单选按钮和金额。以下是截图
谁能指导我为什么单选按钮没有出现在第一行。
【问题讨论】:
标签: listview xamarin.forms observablecollection