How can I use binding with static property ?

I am using this code but its not working :

IsVisible="{x:Static local:GlobalResources.IsUserLoggedIn}"

This is the class :

public class GlobalResources { public static bool IsUserLoggedIn { get; set; } }

Best Answer

Answers

  • fdesbrossesfdesbrosses USMember ✭✭

    What do you mean "not working" ? Not updating when the value changes ? If so, it's normal cause you're not raising PropertyChanged event (GlobalResources should then implementINotifyPropertyChanged interface).

  • mostafamarjimostafamarji USMember

    Yes I am trying to update the value , and I also try to use NotifyPropertyChanged interface but it also doesn't work :

    `public class GlobalResources:INotifyPropertyChanged
    {

        public static bool _isUserLoggedIn { get; set; }
    
        public  bool IsUserLoggedIn
        {
            get { return _isUserLoggedIn; }
            set
            {
                _isUserLoggedIn = value;
                OnPropertyChanged("IsUserLoggedIn");
            }
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged == null)
                return;
    
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }`
    
  • KrishTalaKrishTala INMember ✭✭

    @fdesbrosses
    Yes, its working.
    Thanks for saving my time.

  • dengerelidengereli TRMember ✭✭

    all are you really sure it is working but is it singleton?

    public static GlobalResources Current = new GlobalResources();

    Current will be produce every called. Please change this below.

    private static GlobalResources _current;
    public static GlobalResources Current => _current ?? (_current = new GlobalResources ());

Sign In or Register to comment.