Forum Xamarin.Forms

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

Xamarin Picker Bind Selected Item Detail

I can write the product name from my picker in the txtProduct entry, but I cannot write the price of the selected product in the txtPrice entry section.

Thats my xaml code;

<StackLayout Orientation="Vertical" Padding="30,40,30,24" Spacing="7"> <Entry Placeholder="OrderId" FontSize="16" x:Name="txtId" IsVisible="True"/> <Entry Placeholder="Firma Adı" FontSize="16" x:Name="txtCompanyName"/> <Picker x:Name="myPicker" ItemDisplayBinding="{Binding ProductName}" SelectedItem="{Binding SelectedProduct}" Title="Ürün Seçimi" SelectedIndexChanged="myPicker_SelectedIndexChanged" IsVisible="True"/> <Entry x:Name="entCount" Placeholder="Adet Giriniz" Keyboard="Numeric" IsVisible="True"/> <Entry x:Name="txtProduct" IsVisible="True" IsEnabled="False"/> <Entry x:Name="txtPrice" IsEnabled="False" Text="{Binding SelectedProduct.ProductPrice}"/> <Editor Placeholder="Detaylar" FontSize="16" x:Name="txtDetail"/> <Button Text="Sepete EKle" x:Name="AddBox" Clicked="AddBox_Clicked"/> <Button Margin="0,15,0,0" Text="Sipariş Oluştur" BackgroundColor="#00a8cc" TextColor="White" x:Name="AddOrder" Clicked="AddOrder_Clicked"/> <Button Text="Sepete Git" x:Name="SepeteGit" Clicked="SepeteGit_Clicked"/> <StackLayout>

Thats my cs code;

` protected async override void OnAppearing()
{
base.OnAppearing();
_ = await firebaseHelper.GetAllProducts();
var allProducts = await firebaseHelper.GetAllProducts();
myPicker.ItemsSource = allProducts;
myPicker.SelectedItem = allProducts;
}

    private void myPicker_SelectedIndexChanged(object sender, EventArgs e)

{
var ProductName = myPicker.Items[myPicker.SelectedIndex];
txtProduct.Text = (ProductName);
}`

Thats my class code;

public class Product { public int ProductId { get; set; } public string ProductName { get; set; } public string ProductDetail { get; set; } public string ProductPrice { get; set; } }

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    You could get the selected item like:

    private void myPicker_SelectedIndexChanged(object sender, EventArgs e)
    {
        var selectedProduct = myPicker.SelectedItem as Product;
    
        // Retrieve the other properties here
        txtProduct.Text = selectedProduct.ProductName;
        txtPrice.Text = selectedProduct.ProductPrice;
    }
    

    I noticed that you consumed binding here: <Entry x:Name="txtPrice" IsEnabled="False" Text="{Binding SelectedProduct.ProductPrice}"/>. If you have set a proper binding context, it could display the text correctly.

Sign In or Register to comment.