Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visibility binding on grid not working

Tags:

c#

wpf

mvvm-light

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?

like image 647
Saad Abdullah Avatar asked Oct 28 '25 14:10

Saad Abdullah


2 Answers

Not really answering, but:

  • IMHO, you shouldn't have Visibility enums in your ViewModels. See, ViewModels should be agnostic of the view technology itself (realize, for instance, that INotifyPropertyChanged isn't part of the WPF libraries). So bind, instead, to a boolean and use a converter.
  • Puiblic properties in .NET usually follows Pascal Casing, so, I suggest altering isVisiblePane to IsPaneVisible.
  • Double check your view's DataContext.
  • Run the project in debug mode and watch the console for messages concerning binding.
like image 194
Bruno Brant Avatar answered Oct 31 '25 03:10

Bruno Brant


  • 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 (....)

like image 30
Gary Kaizer Avatar answered Oct 31 '25 04:10

Gary Kaizer