How to hide wpf datagrid columns depending on a property

前端 未结 4 1861
迷失自我
迷失自我 2020-11-29 04:05

I have the following WPF sample program:

Xaml:



        
4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-29 04:33

    Solution proposed by H.B. is really good and has true WPF MVVM spirit. Use it where possible.

    In my particular case something went wrong so I came out with different way, as my project is not strict MVVM, so I can use coded solution.

    In CustomView.xaml name assigned to column:

    
        
            
            ...
    

    In CustomView.xaml.cs we have a simple property which directly changes visibility of column:

    public Visibility MachinesColumnVisible
    {
        get { return MachinesColumn.Visibility; }
        set
        {
            if (value == MachinesColumn.Visibility)
                return;
            MachinesColumn.Visibility = value;
        }
    }
    

提交回复
热议问题