Search code examples
c#wpfmvvm-light

Visibility binding on grid not working


I am trying to bind the visibility of grid but unable to do so.

//ViewModel Class
 private Visibility _isVisiblePane = Visibility.Hidden;
        public Visibility isVisiblePane { 
            get
            {
                return _isVisiblePane;
            }
            set
            {

                _isVisiblePane = value;
                RaisePropertyChanged(() => "isVisiblePane");
            }
        }
//xaml code
<Grid Visibility="{Binding Path=isVisiblePane}">
....My Content....
</Grid>

While debugging, the program sets the value to hidden but when i change the visibility of _isVisiblePane, it doesn't update visibility in GUI(grid remains hidden while _isVisiblePane value is Visible).

//in some function => on button click, value of _isVisiblePane updates to Visible but grid remains hidden.
     isVisiblePane = isLastActiveDoc() == true ? Visibility.Visible : Visibility.Hidden;

Error! on RaisePropertyChanged("isVisiblePane") line. seems like there is no property with this name "An exception of type 'System.ArgumentException' occurred in GalaSoft.MvvmLight.dll but was not handled in user code"

PS: I have tried IValueConverter method with bool too. and still not figuring out whats the problem. Any help?


Solution

    • Make sure that your ViewModel class implements INotifyPropertyChanged.
    • Make sure that RaisePropertyChanged which eventually calls PropertyChanged in the proper thread context if your view is also your UI.

      public event PropertyChangedEventHandler PropertyChanged;
      
      private void RaisePropertyChanged(String info)
      {
          if (PropertyChanged != null)            
              PropertyChanged(this, new PropertyChangedEventArgs(info));            
      }
      
    • Make sure that the Grid either has your VM as it's data context or specify the proper Source for your binding.

    • Make sure that RaisePropertyChanged has anything bound to it via

      if(RaisePropertyChanged != null) RaisePropertyChanged (....)