如何循环ListView并从条目中获取数据? Xamarin表格

时间:2017-07-10 06:04:41

标签: c# listview xamarin.forms

我有一个列表视图,其中包含名为actualmeterreading的列表中的数据。在每一行中都有一个标签和条目,两者都显示列表中的数据。用户可以编辑条目中显示的数据。如何在列表视图中循环并获取用户在条目中输入的数据?

填充列表视图的代码。

 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

                                   }
                                }
                           }
                    }
           };
      })
};

点击提交按钮循环浏览列表视图。

    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);
        }

    }

当我进行一些搜索时,有人提到了View模型和两种绑定方式。有没有人对这个或任何其他解决方案有一些解决方案?谢谢

1 个答案:

答案 0 :(得分:0)

请参阅以下代码以达到您的要求。无论何时编辑条目,它都将存储在模型中,标签也会被反映出来。

         <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;
    }