property binded in custom control not updating

vanKraster 201 Reputation points
2022-06-17T11:32:21.997+00:00

Hi guys,
I have created a custom Switch (derived from Grid) and I have binded the property IsVisible to a bool.
Now I'm saving the values internal in my component that I use in a grid multiple times in the same page.
So when I save the values isVisibleInternal the value is False and the component is hidden, but if anywhere in the code I use isVisibleInternal it is always TRUE . If I call directly this.IsVisible is always true even if the component is Hidden , how it is possible such a thing ?

It is related to the thing that I use it multiple times in the same page ?

   private bool isVisibleInternal = true;  
       private bool isEnabledInternal = true;  
     
       protected override void OnPropertyChanged([CallerMemberName] string propertyName = null)  
       {  
           base.OnPropertyChanged(propertyName);  
     
           if (propertyName == "IsVisible")  
           {  
               isVisibleInternal = IsVisible;  
     
           }  
           else if (propertyName == "IsEnabled")  
           {  
               isEnabledInternal = IsEnabled;  
           }  
       }  

Binded this way

   IsEnabled="{Binding IsBool}"  
          IsVisible="{Binding IsBool}"  
Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,377 questions
{count} votes

2 answers

Sort by: Most helpful
  1. vanKraster 201 Reputation points
    2022-06-21T11:37:53.617+00:00

    Hi Leon,
    Sorry for my late answer but your solution Doesn't work, because when I want to read CurrentControl.IsVisible or CurrentControl.IsEnabled they are always true even if it was setted to false. Again I am sure that is related to fact that I use my control inside a Collectionview ( on each row ! ) and it is reusing same control or something like that. But ! I have found a solution:
    The key is to use that static dictionary , and now the values are correct !

            public static Dictionary<Guid, bool> dicEnabled = new Dictionary<Guid, bool>();  
             public static Dictionary<Guid, bool> dicVisible = new Dictionary<Guid, bool>();  
          
             private bool isVisibleInternal => Id != Guid.Empty && dicVisible.ContainsKey(Id) ? dicVisible[Id] : true;  
                private bool isEnabledInternal => Id != Guid.Empty && dicEnabled.ContainsKey(Id) ? dicEnabled[Id] : true;  
      
     public SsSwitch()  
            {  
                InitializeComponent();  
                Id = Guid.NewGuid();  
            }  
      
      
         protected override void OnPropertyChanged([CallerMemberName] string propertyName = null)  
                {  
                    base.OnPropertyChanged(propertyName);  
          
                    if (propertyName == "IsVisible")  
                    {  
                        if (dicVisible.ContainsKey(Id))  
                            dicVisible[Id] = IsVisible;  
                        else dicVisible.Add(Id, IsVisible);  
          
                        if (!IsVisible)  
                        {  
                            this.RemoveBinding(SsSwitch.IsOnProperty);  
                        }  
                    }  
                    else if (propertyName == "IsEnabled")  
                    {  
                        if (dicEnabled.ContainsKey(Id))  
                            dicEnabled[Id] = IsEnabled;  
                        else dicEnabled.Add(Id, IsEnabled);  
          
                        if (!IsEnabled)  
                        {  
                            this.RemoveBinding(SsSwitch.IsOnProperty);  
                        }  
                    }  
                }  
    
    1 person found this answer helpful.

  2. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 79,546 Reputation points Microsoft Vendor
    2022-06-20T06:13:39.41+00:00

    Hello,​

    You can create a custom Switch (derived from Grid), you can add a name in <Grid>. such as <Grid x:Name="myGrid"> in custom control.
    Then if you want to set this custom control visable or enable. you can set it directly in OnPropertyChanged.

       protected override void OnPropertyChanged([CallerMemberName] string propertyName = null)  
               {  
                   base.OnPropertyChanged(propertyName);  
                   if (propertyName == "IsVisible")  
                   {  
                       myGrid.IsVisible = IsVisible;  
                   }  
                   else if (propertyName == "IsEnabled")  
                   {  
                       myGrid.IsEnabled = IsEnabled;  
                   }  
               }  
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.