Iterate through Buttons

kotetotemkotetotem Member ✭✭

Hey, guys

I am using using Rg.Plugins.Popup.Pages Plugin for showing Popups. I have a problem, when i check checkbox or chnage color of button, when i close Popup and open it again all info on it is lost and set to default. So I am using MessegingCenter to send new and old data between scripts, and that part works.

Problem begins when I want to check checkbox through c# code in my class, or change color of button. So is there a way to iterate through controls (in this case Buttons) in my PopUp View?

And is it possible to check checkboxes using C# code? When I try it like this "chkBox.IsChecked = true;" I get an exception...

Thx!

Best Answer

  • LeonLuLeonLu Xamurai
    edited July 26 Accepted Answer

    If you have many buttons in a view, you could put it in listview. Make the entire view load faster.

Answers

  • LeonLuLeonLu Member, Xamarin Team Xamurai

    Do you want to achieve the result like the following GIF?

    If so, you could add the CheckBox and add a property, then bind the property to the CheckBox

     <CheckBox  IsChecked="{Binding IsChecked, Mode=TwoWay }" Grid.Row="0" Grid.Column="2"/>
    

    And, add here is model to achieve INotifyPropertyChanged.

      public class MyColor : INotifyPropertyChanged
    {
        public Color ColorType { get; set; }
        public string ColorName { get; set; }
        bool _ischecked = false;
        public bool IsChecked {
            get
            {
                return _ischecked;
            }
    
            set
            {
                if (_ischecked != value)
                {
                    _ischecked = value;
                    OnPropertyChanged("IsChecked");
    
                }
    
            } }
    
    
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    

    In the end, We should change the place of load the data in the ObservableCollection before push the popup window.

    Here is my edited demo.

  • kotetotemkotetotem Member ✭✭

    @LeonLu said:
    Do you want to achieve the result like the following GIF?

    If so, you could add the CheckBox and add a property, then bind the property to the CheckBox

     <CheckBox  IsChecked="{Binding IsChecked, Mode=TwoWay }" Grid.Row="0" Grid.Column="2"/>
    

    And, add here is model to achieve INotifyPropertyChanged.

      public class MyColor : INotifyPropertyChanged
    {
        public Color ColorType { get; set; }
        public string ColorName { get; set; }
        bool _ischecked = false;
        public bool IsChecked {
            get
            {
                return _ischecked;
            }
    
            set
            {
                if (_ischecked != value)
                {
                    _ischecked = value;
                    OnPropertyChanged("IsChecked");
    
                }
    
            } }
    
      
       
        public event PropertyChangedEventHandler PropertyChanged;
    
        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    

    In the end, We should change the place of load the data in the ObservableCollection before push the popup window.

    Here is my edited demo.

    Omg, yes!!

    I will try it out when I come home but it defnitly looks like what i meant thx!!

    Just one more question if you are willing to answer, is there a way to loop through my buttons on View?
    Beacuse I think it is not most beautiful solution to set 15 If-s if it can be done with 1 loop?

    thx! :-D

  • LeonLuLeonLu Member, Xamarin Team Xamurai
    edited July 26 Accepted Answer

    If you have many buttons in a view, you could put it in listview. Make the entire view load faster.

  • kotetotemkotetotem Member ✭✭

    @LeonLu said:
    If you have many buttons in a view, you could put it in listview. Make the entire view load faster.

    Awsome, so just repleace buttons with ListView?

    thx man gonan try all that! :-)

  • LeonLuLeonLu Member, Xamarin Team Xamurai

    Ok, waitting for your update.

  • kotetotemkotetotem Member ✭✭

    @LeonLu said:
    Ok, waitting for your update.

    Hey, sorry I forgot to post an answer...
    I decided to go with listview, it made things much more simple since I only have to chose 1 item and remeber it, thx! :-)

Sign In or Register to comment.