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

后端 未结 2 539
清歌不尽
清歌不尽 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.

     <ListView
                x:Name="listaArticulosCarrito"
                BackgroundColor="White"
                ItemsSource="{Binding pricemodels}"
                SelectedItem="{Binding selecteditem}">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <StackLayout Orientation="Horizontal">
                                <StackLayout HorizontalOptions="StartAndExpand">
                                    <Label
                                        Padding="7"
                                        FontSize="Large"
                                        Text="{Binding Producto}"
                                        TextColor="Black" />
                                </StackLayout>
                                <StackLayout HorizontalOptions="EndAndExpand" Orientation="Horizontal">
                                    <Label
                                        x:Name="PrecioProducto"
                                        HorizontalOptions="EndAndExpand"
                                        Text="{Binding Precio}"
                                        TextColor="LightGray"
                                        VerticalOptions="CenterAndExpand" />
                                    <Label
                                        x:Name="CantidadProducto"
                                        Padding="7"
                                        FontSize="Large"
                                        Text="{Binding Cantidad}"
                                        TextColor="Black" />
                                </StackLayout>
                            </StackLayout>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
    

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

     public partial class Page16 : ContentPage, INotifyPropertyChanged
    {
        public ObservableCollection<pricemodel> pricemodels { get; set; }
        private pricemodel _selecteditem;
        public pricemodel selecteditem
        {
            get { return _selecteditem; }
            set
            {
                _selecteditem = value;
                RaisePropertyChanged("selecteditem");
            }
        }
    
        public Page16()
        {
            InitializeComponent();
    
            pricemodels = new ObservableCollection<pricemodel>()
            {
                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;
            }
    
    0 讨论(0)
  • 2021-01-26 05:11

    For example for the label

    <Label x:Name="PrecioProducto"  Text="{Binding Precio}" VerticalOptions="CenterAndExpand"
                                                   TextColor="LightGray" HorizontalOptions="EndAndExpand"/>
    

    In the .cs file you can directly use the name that you set as x:Name(in this case PrecioProducto) as a variable. The text of that label would be

    var tx = PrecioProducto.Text;
    
    0 讨论(0)
提交回复
热议问题