C#/WPF: Make a GridViewColumn Visible=false?

后端 未结 5 655
北恋
北恋 2021-02-13 02:15

Does anyone know if there is an option to hide a GridViewColumn somehow like this:


    
        

        
5条回答
  •  清歌不尽
    2021-02-13 02:29

    One simpler approach, that still uses the concept of setting the columns width to zero but does not have the side effects of using a IValueConverter (the user can still drag the column wider) is to create a new getter/setter that returns a width based on your ColumnIsVisible variable and then bind to that:

    public double ColumnWidth
    {
        get
        {
            if (this.ColumnIsVisible)
            {
                return 100;
            }
            else
            {
                return 0;
            }
        }
    
        set
        {
            OnPropertyChanged("ColumnWidth");
        }
    }
    

    Make your bindings TwoWay and if the user attempts to drag the column wider OnPropertyChanged will be called and reset the width to 0. You might have to use a binding proxy though for your binding. Also add a call to OnPropertyChanged("ColumnWidth") when ever ColumnIsVisible is updated :)

提交回复
热议问题