Shahid Od
Shahid Od

Reputation: 123

Binding Label's Text to a Variable in Xamarin forms c#

So Im trying to creat a simple app like a shopping app. so I have categories and multiple items for each category, and when you get to choose an item then you will have the posibility to increase how many you need or delete the item. For exemple I chosed three items, so my cart have 3 items where each one have an Add button and a delete button. When I hit the add button the number of the items shown should increase and so on.

so what I've done so far is creating a JSON file that having all my categories, and once I hit a category I get to deserialize another JSON file that have all my items, so the items shown depends on the category I chosed of course.

Now each time i choose an item it get added to the cart and shown on the bottom page with a + and - buttons and so on.

so I created a category class to deserialize my json, and an objets class to deserialize my Item's json. I implememted the INotifyChangedProperty in the objets class so that I can keep showin whenever the number of a chosen item get increased, so basicly thats my ViewModel, but I guess that it's like that I need a ViewModel of each created item ? so I guess what I really need to use is the ObservableCollection ..

I hope I explained everything well, and waiting for your feedbacks about if Im doing it right or wrong and how should i proceed to get what I want. thank you so much

Upvotes: 1

Views: 7350

Answers (3)

Cherry Bu - MSFT
Cherry Bu - MSFT

Reputation: 10346

the problems is that to set the bindingcontext to my "Objets" Class I have to put the arguments in it, and then my Label well get a precised value ... what should I do ?

I have written a sample for your model, you can take a look:

<ContentPage.Content>
    <StackLayout>
        <Label x:Name="label1" />

        <Button
            x:Name="btn1"
            Clicked="Btn1_Clicked"
            Text="change value" />
    </StackLayout>
</ContentPage.Content>


public partial class Page15 : ContentPage
{
    public Objets model { get; set; }
    public Page15()
    {
        InitializeComponent();
        model= new Objets("test 1", 1.001f, " test11111", 12);
        this.BindingContext = model;
        label1.SetBinding(Label.TextProperty, "nbr_objet");
    }

    private void Btn1_Clicked(object sender, EventArgs e)
    {
        model.nbr_objet = 20;
    }
}
public class Objets : INotifyPropertyChanged
{
    public string Designation { get; set; }
    public float Prix { get; set; }
    public string imageUrl { get; set; }
    private int Nbr_Objet;

    public int nbr_objet
    {
        get { return Nbr_Objet; }
        set
        {
            Nbr_Objet = value;
            RaisePropertyChanged("nbr_objet");
        }
    }

   

    public Objets(string Designation, float Prix, string imageUrl, int Nbr_Objet)
    {
        this.Designation = Designation;
        this.Prix = Prix;
        this.imageUrl = imageUrl;
        this.Nbr_Objet = Nbr_Objet;
    }
    
    public event PropertyChangedEventHandler PropertyChanged;

   
    public void RaisePropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

Update:

but I guess that it's like that I need a ViewModel of each created item ? so I guess what I really need to use is the ObservableCollection ..

You said that you have three categories, and each category have many items, If you display these in ListView, category is used as Group header, and I suggest you can use the same model for different item for different categories, then add in Observablecollection, because it have implemented INotifyPropertyChanged interface.

About ListView group, you can take a look:

https://github.com/xamarin/xamarin-forms-samples/tree/master/UserInterface/ListView/Grouping

If you still have another question, I suggest you can create new thread to ask, because this thread is very long.

Upvotes: 2

slugster
slugster

Reputation: 49974

This is some guidance that might help with your problem. Your code is messy and I think that is causing your confusion (you have several things named very similarly).

int Nbr_Objet;

public int nbr_objet { get{...} set {...}}

this.Nbr_Objet= Nbr_Objet;

this shows me that you are setting your member variable Nbr_Objet directly, when you do that the property change notification doesn't fire - you need to assign the value through the public nbr_objet for that to happen.

I'd suggest you define the binding in XAML, and make sure you bind to the property nbr_objet, not the private member variable (field) Nbr_Objet. If you want to avoid confusion, follow the C# coding standard and name your member variable _nbrObjet, and camel case your property name public int NbrObjet { get {....

Upvotes: 0

Jason
Jason

Reputation: 89082

to set a binding programatically

// set the BindingContext for the page
this.BindingContext = new MyViewModel();

// Title is a public property on MyViewModel
myLabel.SetBinding(Label.TextProperty, "Title");

in order for the UI to update when the VM is changed, the VM needs to implement INotifyPropertyChanged

Upvotes: 0

Related Questions