Xamarin Forms为listview的每个项添加输入字段

时间:2017-03-18 18:50:37

标签: c# xamarin.forms

我是Xamarin Forms的新手,我想在listview的每个项目的右侧添加一个输入字段(数字),以便我可以为我的项目键入一个quanty。就像物品的购物清单,有你想要的数量。

这是我的listview代码

List<string> item = new List<string>();

    public MainPage()
    {
        InitializeComponent();


        item.Add("Apple");
        item.Add("Banana");
        item.Add("Graps");
        item.Add("Orange");
        item.Add("Pineapple");
        item.Add("Strawberry");
        item.Add("Lemon");
        item.Add("Mango");
        item.Add("Cherry");
        item.Add("Watermelon");
        item.Add("Add");

        var listView = new ListView
        {
            RowHeight = 40
        };

        listView.ItemsSource = item;

        StackLayout layout = new StackLayout();

        layout.Children.Add(listView);



        listView.ItemSelected += (sender, e) =>
        {
            if (e.SelectedItem.ToString() == "Add")
            {
                var MyEntry = new Entry { Placeholder = "new item" };
                layout.Children.Add(MyEntry);
                MyEntry.Completed += MyEntry_Completed;

            }
        };

        this.Content = layout;

    }

    private void MyEntry_Completed(object sender, EventArgs e)
    {
        var text = ((Entry)sender).Text;
        item.Add(text);

    }

1 个答案:

答案 0 :(得分:1)

我在Xamarin Forms的C#端数据绑定中遇到了很多麻烦。我建议您结帐DataBinding以便更好地了解这一点。我创建了一个自定义ViewCell供您使用,以及一个基本实现,以便您可以直观地了解如何实现它。您可以根据需要调整视图单元格。我只想给你一个如何实现它的基础。希望这可以帮助。

<强> ViewCell
这是为列表视图中的每个列表项显示的视图。

using Xamarin.Forms;

namespace BountyApp.Controls
{
    public class CustomViewCell : ViewCell
    {
        private Grid _grid = new Grid();
        private Label _lbl = new Label() { HorizontalTextAlignment = TextAlignment.End, VerticalTextAlignment = TextAlignment.Center };
        private Entry _entry = new Entry();


        public CustomViewCell()
        {
            _lbl.SetBinding(Label.TextProperty, new Binding("Title"));
            _grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(0.3, GridUnitType.Star) });
            _grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(0.7, GridUnitType.Star) });

            _grid.Children.Add(_lbl, 0, 0);
            _grid.Children.Add(_entry, 1, 0);

            View = _grid;
        }

    }
}

实施ViewCell

using Xamarin.Forms;

namespace BountyApp.Pages
{
   public class ViewModel
    {
        public string Title { get; set; }
    }
    public class StepperPage : ContentPage
    {
       public ObservableCollection<ViewModel> List { get; set; }

        public StepperPage()
        {
            List = new ObservableCollection<ViewModel>();
            List.Add(new ViewModel { Title = "Apple"      });
            List.Add(new ViewModel { Title = "Banana"     });
            List.Add(new ViewModel { Title = "Graps"      });
            List.Add(new ViewModel { Title = "Orange"     });
            List.Add(new ViewModel { Title = "Pineapple"  });
            List.Add(new ViewModel { Title = "Strawberry" });

            var listView = new ListView
            {
                RowHeight = 40, 
                ItemTemplate = new DataTemplate(typeof(CustomViewCell)),
                ItemsSource  = List
            };


            Content = listView;
        }
    }
}

<强>结果

enter image description here