How can I get the Text from a Label inside a ListView?

后端 未结 2 540
清歌不尽
清歌不尽 2021-01-26 04:26

I\'m using Xamarin.forms in visual studio. The problem that I have is that I don\'t know how to get the Text that is shown on a Label inside a List view in the xaml file, becaus

2条回答
  •  醉话见心
    2021-01-26 05:04

    According to your description, I am not sure if you want to get PrecioProducto Label text for ListView selected item, or you want to get all PrecioProducto Label text for ListView.

    If it is the first case, as Jason's opinion, I suggest you can using binding for ListView SelectedItem.

     
                
                    
                        
                            
                                
                                    
                                
                                    
                            
                        
                    
                
            
    

    Please don't forget to implement INotifyPropertyChanged interface to notify data changed.

     public partial class Page16 : ContentPage, INotifyPropertyChanged
    {
        public ObservableCollection pricemodels { get; set; }
        private pricemodel _selecteditem;
        public pricemodel selecteditem
        {
            get { return _selecteditem; }
            set
            {
                _selecteditem = value;
                RaisePropertyChanged("selecteditem");
            }
        }
    
        public Page16()
        {
            InitializeComponent();
    
            pricemodels = new ObservableCollection()
            {
                new pricemodel(){Producto="product 1",Precio=21.01,Cantidad="product1"},
                new pricemodel(){Producto="product 2",Precio=31.01,Cantidad="product2"},
                new pricemodel(){Producto="product 3",Precio=41.01,Cantidad="product3"},
                new pricemodel(){Producto="product 4",Precio=51.01,Cantidad="product4"},
                new pricemodel(){Producto="product 5",Precio=61.01,Cantidad="product5"}
            };
            this.BindingContext = this;
        }
    
        public event PropertyChangedEventHandler PropertyChanged;      
        public void RaisePropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        private void Button_Cicked(object sender, EventArgs e)
        {
    
            var PrecioProducto = selecteditem.Precio;
            Console.WriteLine("the listview select item precioproduct label text is {0}",PrecioProducto);
        }
    }
    
    public class pricemodel
    {
        public string Producto { get; set; }
        public double Precio { get; set; }
        public string Cantidad { get; set; }
    }
    

    If it is the second, you want to get all PrecioProducto Label text for ListView, you just foreach pricemodels to get Precio value.

     foreach(pricemodel m in pricemodels)
            {
                var precio = m.Precio;
            }
    

提交回复
热议问题