Bind visibility property to a variable

前端 未结 5 742
伪装坚强ぢ
伪装坚强ぢ 2020-12-05 14:12

I have a Border with Label inside a Window,



        
5条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-05 14:20

    You don't need to make any converter.

    Add a binding to a Visibility property for the border:

    
        
            
    
    

    And then create the property Visibility in a viewmodel like this:

    private Visibility visibility;
    public Visibility Visibility
        {
            get
            {
                return visibility;
            }
            set
            {
                visibility = value;
    
                OnPropertyChanged("Visibility");
            }
        }
    

    So, now you can set Visible or Hidden to your Visibility property as follow:

    Visibility = Visibility.Visible;
    // or
    Visibility = Visibility.Hidden;
    

    But remember, Visibility enum is located in System.Windows namespace, so your viewmodel has to contain using System.Windows;.

提交回复
热议问题