【问题标题】:How to loop through a ListView and get the data from entry? Xamarin Forms如何遍历 ListView 并从条目中获取数据? Xamarin 表单
【发布时间】:2017-07-10 19:15:39
【问题描述】:

我有一个列表视图,其中包含来自名为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 模型和两种方式绑定。有没有人有关于那个或任何其他解决方案的解决方案?谢谢

【问题讨论】:

  • 最好的方法是使用 MVVM 数据绑定,如果您不愿意这样做,只需为 Entry 编写一个 Text changed 事件并在那里更新源项

标签: c# listview xamarin.forms


【解决方案1】:

请参考以下代码来满足您的要求。每当编辑条目时,它都会存储在模型中,标签也会得到反映。

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

【讨论】:

  • 感谢您的想法,但是当我尝试在循环中访问 model.Name 时,它​​仍然给我旧值,而不是存储在模型中的值。我在我的 itemsource 列表中循环。
  • @batwing 新闻?你找到解决办法了吗
猜你喜欢
  • 1970-01-01
  • 2012-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-26
  • 2021-08-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多