1
<Label x:Name="lbl" Text="{Binding Source={Static local:ViewModel.Texty}}"

Here's ViewModel Class

public class DownloadDataPageViewModel : INotifyPropertyChanged
{
    public static event PropertyChangedEventHandler StaticPropertyChanged;

    private static void OnStaticPropertyChanged(string propertyName)
    {
        StaticPropertyChanged?.Invoke(null, 
            new PropertyChangedEventArgs(propertyName));
    }

    private static string _texty;

    public static string Texty
    {
        get => _texty;
       
        set
        {
            _texty = value;
            OnStaticPropertyChanged("Texty");
        }
    }
}

The Problem that i'm facing is that it Label is showing "StaticCheckApp.ViewModel" instead of change in data.

Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
  • `INotifyPropertyChanged.PropertyChanged` event in not invoked. – Pavel Voronin Jul 25 '21 at 10:35
  • 1
    This is not how you should use`INotifyPropertyChanged`. It is designed to notify property changes in INSTANCE PROPERTIES not static properties! Additionally `Binding` cares about property change notifications inside the `Path` of the `Binding`. Its `Source` will get computed once and will never change if I'm not mistaken. – Pharaz Fadaei Jul 25 '21 at 10:43
  • I'm not Sure how to Achieve this, How should i Bind to static Resource. I went through online Docs and this is best that i could get at i guess. – Prabhat Kumar Jul 25 '21 at 10:45
  • 1
    This might help: [another question](https://stackoverflow.com/questions/14614190/inotifypropertychanged-and-static-properties) – Pharaz Fadaei Jul 25 '21 at 10:47
  • Your Texty update code might help. – Shaw Jul 25 '21 at 11:12
  • nothing, it's just a Viewmodel.Texty = "1002"; – Prabhat Kumar Jul 25 '21 at 16:00
  • You seem to have simply used the wrong syntax. Assuming Xamarin's XAML syntax is the same as WPF, your binding should be `{Binding Path=(local:ViewModel.Texty)}`, and your question would be a duplicate of [this one](https://stackoverflow.com/questions/68448604/why-i-cant-bind-static-property-with-notification-in-net-core) – Peter Duniho Jul 26 '21 at 03:28

1 Answers1

0

To implement data changed notify, you need to implement INotifyPropertyChanged Interface in Xamarin.forms.

Unfortunately, static classes cannot implement interfaces, so your solution will not work.

Usually, I create class and implementing INotifyPropertyChanged interface.

 public class DownloadDataPageViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public void RaisePropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    private string _texty;
    public string Texty
    {
        get => _texty;

        set
        {
            _texty = value;
            RaisePropertyChanged("Texty");
        }
    }
}

<ContentPage.Resources>
    <local:DownloadDataPageViewModel x:Key="model1" Texty="this is test" />

</ContentPage.Resources>
<ContentPage.Content>
    <StackLayout>
        <Label
            HorizontalOptions="CenterAndExpand"
            Text="{Binding Texty, Source={StaticResource model1}}"
            VerticalOptions="CenterAndExpand" />
    </StackLayout>
</ContentPage.Content>

If you still want to use static property, you can take a look:

Can't bind static property in XAML (Xamarin Forms)

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