简体   繁体   English

如何遍历ListView并从条目中获取数据? Xamarin形式

[英]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.

相关问题 在 ListView Xamarin 表单中获取条目值 - Get Entry value in ListView Xamarin forms 如何通过视图模型中的 mvvm 通过 selecteditem 显示从列表视图到 xamarin 中的条目的数据? - how to show data from listview to entry in xamarin by selecteditem through mvvm in viewmodel? Xamarin.Forms在ListView中的条目更改时获取ListView的对象 - Xamarin.Forms Get the object of a ListView when an Entry in the ListView is changed 如何从 xamarin forms 中的列表视图中获取所选项目索引? - How to get the selected item index from the listview in xamarin forms? 如何从Xamarin Forms中的ListView集合获取总价 - How To Get Total Prices From ListView Collection in Xamarin Forms 循环遍历 4 个对象列表并将它们的数据添加到 ListView C# Xamarin.Forms - Loop through 4 object lists & add their data in a ListView C# Xamarin.Forms 如何通过索引从 xamarin 形式的 ListView 对象获取访问视单元? - How to get access viewcell by index from ListView object in xamarin forms? Xamarin.forms:如何从 Listview 获取对象值 - Xamarin.forms: How to get Object value from a Listview 如何从 Xamarin Forms 中的自定义 ViewCell 获取 ListView 项目索引? - How to get ListView item index from custom ViewCell in Xamarin Forms? Xamarin Forms-如何从输入的数据填充ListView? - Xamarin Forms - How to populate ListView from the data you entered?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM