【问题标题】:How can I get the Text from a Label inside a ListView?如何从 ListView 内的标签中获取文本?
【发布时间】:2020-04-22 17:35:00
【问题描述】:

我在 Visual Studio 中使用 Xamarin.forms。我遇到的问题是我不知道如何获取在 xaml 文件的列表视图内的标签上显示的文本,因为我想使用该文本对 xaml 中的另一个列表进行一些更改。 .cs 文件。

    <?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>

我的 xaml 文件以及我想要在 xaml.cs 文件中进行更改的值是名为“PrecioProducto”的标签中的文本。如果你能告诉我代码是如何用 c# 编写的,那真的会对我有所帮助。

【问题讨论】:

  • 该标签的文本在您的模型中设置为属性Precio,因此您可以参考您的模型来获取该值
  • 非常感谢,这对我打算做的另一件事很有帮助。
  • 你的意思是把listView中的绑定项显示到另一个标签上吗?

标签: c# xaml xamarin


【解决方案1】:

例如对于标签

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

在 .cs 文件中,您可以直接使用您设置为 x:Name(在本例中为 PrecioProducto)的名称作为变量。 该标签的文本将是

var tx = PrecioProducto.Text;

【讨论】:

  • 谢谢,但我无法用那个解决方案解决它。
【解决方案2】:

根据您的描述,我不确定您是要获取 ListView 所选项目的 PrecioProducto Label 文本,还是要获取 ListView 的所有 PrecioProducto Label 文本。

如果是第一种情况,按照 Jason 的意见,我建议您可以对 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>

请不要忘记实现 INotifyPropertyChanged 接口来通知数据更改。

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

如果是第二个,你想获取 ListView 的所有 PrecioProducto Label 文本,你只需 foreach pricemodels 获取 Precio 值。

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

【讨论】:

    猜你喜欢
    • 2021-06-09
    • 1970-01-01
    • 2015-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多