Here I want to show you how to create a 'Subscribe' button that changes text value (Subscribe, Unsubscribe) in a ListView.
First of all, you create your oun button, in this button class you add a BindableProperty that will help us to change the text value of the button :) It should be like this.
In this exemple we use in value, you can use a BOOL type.
In this exemple we use in value, you can use a BOOL type.
- 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);
- }
- }
Now, we change the text value when this property's value is changed. So, our Button looks 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;
- }
- }
- }


Alain NataliniPosted Aug 25, 2017, 2:10 AM
Good article. Thank you.