-1

I have an issue with the property IsVisible within a label. If the text was immediate (a normal string) and the value of IsVisible is true it would work properly, but if I used a binded text or the property IsVisible binded it would not work. Please help.

<Label x:Name="st" Text="{Binding status}" HorizontalOptions="Start" FontSize="Medium" 
BindingContext="{x:Reference sw}"  IsVisible="{Binding IsToggled}" />

<Switch x:Name="sw"  IsToggled="True"  HorizontalOptions="EndAndExpand"  />
Jan Nepraš
  • 794
  • 1
  • 7
  • 29
a. 1
  • 13
  • 3
  • 1
    please do NOT post code or errors as images. Take the time to copy the code and properly format it. – Jason May 09 '20 at 20:29
  • Would you mind to edit your question and be more specific? You can read this article https://stackoverflow.com/help/how-to-ask. From my perspective you might provide us how you setup binding. – Jan Nepraš May 11 '20 at 07:10

1 Answers1

0

First of all, please accept Jason's suggestion, take some time to copy your code and format it ptoperly.

According to your code, don't binding Label St BindingContext to Switch Sw, because you binding Label St Text to status, but Switch Sw don't have this property, so please change your code.

<Label
            FontSize="Medium"
            HorizontalOptions="StartAndExpand"
            IsVisible="{Binding Source={x:Reference sw}, Path=IsToggled}"
            Text="{Binding status}" />
        <Switch
            x:Name="sw"
            HorizontalOptions="EndAndExpand"
            IsToggled="True" />

 public partial class Page12 : ContentPage, INotifyPropertyChanged
{
    private string _status;
    public string status
    {
        get { return _status; }
        set
        {
            _status = value;
            RaisePropertyChanged("status");
        }
    }

    public Page12()
    {
        InitializeComponent();

        status = "this is test";
        this.BindingContext = this;
    }

    public event PropertyChangedEventHandler PropertyChanged;      
    public void RaisePropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

Note: don't forget to implement INotifyPropertyChanged interface, to notify data changed.

About binding, please take a look:

https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/data-binding/basic-bindings

Cherry Bu - MSFT
  • 10,160
  • 1
  • 10
  • 16