How do I CHANGE a binding in a xaml page, from a .cs page after a button is pressed? please help :)

ThankYouThankYou Member ✭✭

I have several switches, when they are turned on, I want to change the binding where it says "here" in the xaml page from the .cs page, and change the binding to another binding which I have control on, so all I need help with is, how do I refer to the binding and change it in the .cs page after the switch is turned on? Please give me any advice you have, thank you so much!

The stack layout in which the binding is, is called "aa" in the x:name in xaml, but whenever I type "aa" in the .cs page, nothing comes up, pls let me know if you have any advice on how to refer to AND change this binding whenever a switch is turned on.

Best Answers

Answers

  • SoniaDevRootSoniaDevRoot Member ✭✭

    when you are binding value under a listCell it wont accept it only when you add a reference, also the x:name of any item under a viewcell will not accept it, you should add a property Here with get and set and you should use your listView name and you can do this
    IsVisible="{Binding Source={x:Reference ListProduct}, Path=BindingContext.Here}"

    and your property
    private bool _here;
    public bool Here
    {
    get { return _here; }
    set { _here= value; }
    }

    do in the code whatever you like

  • ThankYouThankYou Member ✭✭

    @SoniaDevRoot said:
    when you are binding value under a listCell it wont accept it only when you add a reference, also the x:name of any item under a viewcell will not accept it, you should add a property Here with get and set and you should use your listView name and you can do this
    IsVisible="{Binding Source={x:Reference ListProduct}, Path=BindingContext.Here}"

    and your property
    private bool _here;
    public bool Here
    {
    get { return _here; }
    set { _here= value; }
    }

    do in the code whatever you like

    @SoniaDevRoot I formulated my question a bit stupid, since I actually want to change the "here" binding after the switch cell is turned on. I want to change the "here" bool value, to another value that I already have set, would you know how to do so? Thank you so much for help!

  • SoniaDevRootSoniaDevRoot Member ✭✭

    I just want to understand exactly what you need, you mean to change the visibility of the stack whenever you toggle the switcher, is that what you want??

  • SoniaDevRootSoniaDevRoot Member ✭✭

    or you mean that the property that you will bind with will change to another property when the switcher is on??

  • ThankYouThankYou Member ✭✭

    @SoniaDevRoot the binding "here" is what i want to change to {binding Luck} after a button is pressed, so where it says "{binding here}" I want it to be binded to "Luck"(bool value i have already made) after a button or switch is turned on. Hope you understand. :)

  • ThankYouThankYou Member ✭✭

    @SoniaDevRoot said:
    or you mean that the property that you will bind with will change to another property when the switcher is on??

    Yes, I want to change the property to another property after a switch is turned on, so it is binded to another bool value! Do you know how to do that? Thank you so much for help, my friend! @SoniaDevRoot

  • SoniaDevRootSoniaDevRoot Member ✭✭

    Ok now I understand , the fisrt thing that come to my mind is that since you are working in a listView you can't give the stack a x:name and that means w can't chage the bindidngcontext by simply calling iit in the codeBehind, so what I think is to create all the cellView in the code behind, you can create a method with the name of the binding as in input and every time you click the button or toggle the switcher you can clear the cells of the list and create it with another binding.
    I don't know if you are familiar with creating views in the code behind, or if you did understand the idea?

  • ThankYouThankYou Member ✭✭

    @SoniaDevRoot I understand what you mean, but how do I start? AND are you sure I can't change that binding in any way? It is really the best solution for my whole project, please, if you have any advice or ideas on how I can change the binding, even if it is hard, please tell me. If not, could you be so kind to lead me in the right direction with your idea? Thanks for the help :)

  • ThankYouThankYou Member ✭✭

    @SoniaDevRoot WAIT! If I can give it an x:name, would it work then? I can try to use the isvisible binding on items further up in the listview, like on the viewcell or something
    Maybe do the isvisible binding on this? I think I can x:name this one > @SoniaDevRoot said:

    Ok now I understand , the fisrt thing that come to my mind is that since you are working in a listView you can't give the stack a x:name and that means w can't chage the bindidngcontext by simply calling iit in the codeBehind, so what I think is to create all the cellView in the code behind, you can create a method with the name of the binding as in input and every time you click the button or toggle the switcher you can clear the cells of the list and create it with another binding.
    I don't know if you are familiar with creating views in the code behind, or if you did understand the idea?

  • SoniaDevRootSoniaDevRoot Member ✭✭

    I've searchd around and I found some answers like this one
    https://stackoverflow.com/questions/48607818/how-to-use-xname-of-items-in-listview-at-codebehind

    also this one talks about this issue
    https://forums.xamarin.com/discussion/84491/how-to-access-the-listview-in-viewcell-c-code-behind

    you can see better why it's hard to change the binding , if these won't help then will get to my idea.

  • ThankYouThankYou Member ✭✭

    @SoniaDevRoot I see why it's hard, if I use "MVVM" it would maybe have worked, but I don't know anything about it, so can I please hear your idea? Thanks :smile:

  • seanydaseanyda GBMember ✭✭✭✭✭

    Are you asking how to set a Binding in C# instead of XAML? If so

    Button button = new Button();
    button.SetBinding(Button.ClassIdProperty, "PROPERTY");
    
  • ThankYouThankYou Member ✭✭

    @seanyda I want to set the binding in the xaml page, from the .cs page (I want to set the new binding after a switch is turned on true).

Sign In or Register to comment.