[英]How to loop through a ListView and get the data from entry? Xamarin Forms
I have a listview with data from a list called actualmeterreading. 我有一个列表视图,其中包含来自称为Actualmeterreading的列表中的数据。 In each row, there are a label and entry, both display data from the list.
每行中都有一个标签和条目,均显示列表中的数据。 User can edit the data displayed in entry.
用户可以编辑条目中显示的数据。 How can I loop through the listview and get the data that user input in the entry?
如何遍历列表视图并获取用户在条目中输入的数据?
Code for populating the listview. 用于填充列表视图的代码。
list = new ListView
{
ItemsSource = actualmeterreading,
RowHeight = 50,
ItemTemplate = new DataTemplate(() =>
{
printDesc = new Label();
string desc = actualmeterreading[x].MachineMeterReadingList.Meterreadingdescription.Description;
printDesc.Text = desc;
meterreading = new Entry();
string reading = actualmeterreading[x].ActualReading.ToString();
meterreading.Text = reading;
meterreading.FontSize = 14;
x = x + 1;
//nameLabel2.WidthRequest = 300;
//nameLabel2.SetBinding(Entry.TextProperty, "");
// Return an assembled ViewCell.
return new ViewCell
{
View = new StackLayout
{
Padding = new Thickness(0, 5),
Orientation = StackOrientation.Horizontal,
Children =
{
new StackLayout
{
VerticalOptions = LayoutOptions.Center,
Spacing = 0,
Children =
{
printDesc,
meterreading
//nameLabel2
}
}
}
}
};
})
};
Loop through the listview on submit button click. 单击提交按钮,循环遍历列表视图。
private void submitbtn_Clicked(object sender, EventArgs e)
{
int x = 0;
foreach (var c in list.ItemsSource)
{
int r = c.?
myactualreading = new actualmeterreading
{
ActualReading = r
};
x = x + 1;
dataservice.UpdateActualReading(myactualreading);
}
}
When I did some search, there was someone who mention to View models and two ways bindings. 当我进行搜索时,有人提到了View模型和两种绑定方法。 Does anyone have some solution regarding that one or any other solutions?
是否有人对此有任何其他解决方案? Thank you
谢谢
Refer the below code to achieve your requirement. 请参考以下代码以实现您的要求。 Whenever the entry is edited, then it will be stored in model and label also will get reflected.
无论何时编辑该条目,它都会存储在模型中,并且标签也会得到反映。
<ListView ItemsSource="{Binding Elements}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Entry Grid.Column="0" Text="{Binding Name,Mode=TwoWay}" />
<Label Grid.Column="1" Text="{Binding Name,Mode=TwoWay}" />
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
public partial class MainPage : ContentPage
{
ObservableCollection<Model> elements;
public ObservableCollection<Model> Elements
{
get { return elements;}
set { elements = value;}
}
public MainPage()
{
Elements = new ObservableCollection<Model>() { new Model() { Name = "One" }, new Model() { Name = "Two" } };
BindingContext = this;
InitializeComponent();
}
}
public class Model : INotifyPropertyChanged
{
string name;
public string Name
{
get { return name; }
set {
name = value;
NotifyPropertyChanged("Name");
}
}
private void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.