How to change Button Content Inside ListView based on bit values.

Im_lavanIm_lavan USMember ✭✭

Hello Folks,
I have a button inside list view by default the Text will be "Subscribe" if i tap on button i need to change the button Text into unsubscribe (I'm getting bit value from service.) and also vice versa using MVVM pattern . Please share code piece of code for the same.

Thanks In Advance

Answers

  • MabroukMabrouk USMember ✭✭✭

    Hello @Im_lavan ,

    Here you can find what you Need : http://www.c-sharpcorner.com/blogs/how-to-create-a-subscribe-button-in-your-xamarinforms-mobile-app

    I hope that helps,
    Mabrouk.

  • MabroukMabrouk USMember ✭✭✭
    edited August 18

    @Im_lavan
    See next post :smile:

  • MabroukMabrouk USMember ✭✭✭

    @Im_lavan
    if the link doesn't work,

    you can get the solution here.

    In General you need to create your oun Subscribe button like :

    public class SubscribeBtn : Button  
                {  
                    public static readonly BindableProperty IsRegistredProperty =  
                        BindableProperty.Create("IsRegistred", typeof(int), typeof(SubscribeBtn), 0);  
              
                    public int IsRegistred  
                    {  
                        get { return (int)GetValue(IsRegistredProperty); }  
                        set  
                        {  
                            SetValue(IsRegistredProperty, value);  
                        }  
                    }  
              
                    private string btnText => IsRegistred == 1 ? "Unsubscribe" : "Subscribe";  
              
                    public SubscribeBtn()  
                    {  
                        this.Text = btnText;  
                        PropertyChanged += SubscribeBtn_PropertyChanged;  
                        Clicked += SubscribeBtn_Clicked;  
                    }  
              
                    private void SubscribeBtn_Clicked(object sender, EventArgs e)  
                    {  
                        IsRegistred = 1 - IsRegistred; // 1 --> 0  // 0 --> 1  
                    }  
              
                    private void SubscribeBtn_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)  
                    {  
                        if (e.PropertyName == IsRegistredProperty.PropertyName)  
                        {  
                            this.Text = btnText;  
                        }  
                    }  
                }
    

    than you can use it,

    I hope that helps,
    Mabrouk.

Sign In or Register to comment.