[英]Checkboxes inside a list view - Xamarin.Forms
I'm working on a Xamarin.Forms app. 我正在开发Xamarin.Forms应用程序。 My requirement is to display a list of objects which are available as Json and add a checkbox to each of the list item, when the item is checked I want to store the item in a separate local DB for future use.
我的要求是显示一个可用作Json的对象列表,并为每个列表项添加一个复选框,当选中该项目时,我想将该项目存储在单独的本地DB中以备将来使用。
This is my item template. 这是我的项目模板。
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid VerticalOptions="Center">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="3*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<controls:CheckBox x:Name="Completed" />
<Label x:Name="index"
HorizontalOptions="Center"
Text="{Binding index}"
Grid.Column="1"/>
<Label x:Name="title"
Text="{Binding title}"
Grid.Column="2" />
<Label x:Name="count"
HorizontalOptions="Center"
Text="{Binding count}"
Grid.Column="3" />
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
this is the item source for the ListView
. 这是
ListView
的项目源。
SurahNames.ItemsSource = viewModel.surahs;
My questions are: 我的问题是:
CheckBox
? CheckBox
的布尔值? You can bind the selected item like this 您可以像这样绑定选定的项目
private object _selectedItem;
public object SelectedItem
{
get { return _selectedItem; }
set
{
cast to your model
YourModel val=(YourModel)value;
//checked is the new proprety that you added to ur model
val.Checked=true;
dosmthwithvalue(val);
//this to unselect the item in listview /remove color
_selectedItem = null;
OnPropertyChanged("SelectedItem");
}
}
and for the CheckBox
you need to add checked property to your model. 对于
CheckBox
您需要将选中的属性添加到模型中。
I didn't tested it and maybe this not the optimal solution, but I think that it will do what you want. 我没有测试过它,也许这不是最佳解决方案,但是我认为它会做您想要的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.