问题
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, because I want to use that text to make some changes to another list in the xaml.cs file.
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Saansa.Views.CarritoDeVentas">
<ContentPage.Content>
<StackLayout BackgroundColor="Blue">
<ListView x:Name="listaArticulosCarrito" BackgroundColor="White">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Horizontal">
<StackLayout HorizontalOptions="StartAndExpand">
<Label Text="{Binding Producto}" Padding="7"
TextColor="Black" FontSize="Large"/>
</StackLayout>
<StackLayout Orientation="Horizontal" HorizontalOptions="EndAndExpand">
<Label x:Name="PrecioProducto" Text="{Binding Precio}" VerticalOptions="CenterAndExpand"
TextColor="LightGray" HorizontalOptions="EndAndExpand"/>
<Label x:Name="CantidadProducto" Text="{Binding Cantidad}" Padding="7"
TextColor="Black" FontSize="Large"/>
</StackLayout>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<StackLayout BackgroundColor="Aqua" Orientation="Horizontal">
<StackLayout BackgroundColor="Red">
<Label Text="Precio total de la venta:" HorizontalOptions="StartAndExpand"
Padding="7"/>
</StackLayout>
<StackLayout BackgroundColor="Yellow" HorizontalOptions="EndAndExpand">
<Label Text="{Binding price}" Padding="7"/>
</StackLayout>
</StackLayout>
<Button Text="Pagar" BackgroundColor="White" Clicked="Button_Clicked"
BorderColor="Orange" BorderWidth="2" CornerRadius="15" Margin="10"/>
</StackLayout>
</ContentPage.Content>
</ContentPage>
That my xaml file, and the value that I want to take so I can make changes in the xaml.cs file is the Text from the label named "PrecioProducto". It would really help me if you can tell me how the code is write in c#.
回答1:
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;
回答2:
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;
}
来源:https://stackoverflow.com/questions/61371471/how-can-i-get-the-text-from-a-label-inside-a-listview