Search code examples
c#wpfbinding

How to hide GridViewColumn using Data trigger in wpf?


Hiding using Width would only hide it Visually, element is still there, what about screen reader.

  • I have GridViewColumn, where I want to show and hide based upon Property, How can I hide it.

  • Currently, I have using the this solution, However I want to do it using Property. How can I achieve it?

<GridViewColumn
                                CellTemplate="{StaticResource KeyTamplate}"
                                Header="{res:Label {x:Static res:enum.LabelName}}"
                                HeaderTemplate="{DynamicResource KeyHere}"
                                 />

Solution

  • Bind the Width property to a double property of the view model that you set to 0 to hide the column:

    <GridViewColumn Header="header..." DisplayMemberBinding="{Binding Property}"
         Width="{Binding ColumnWidth}" />
    

    View Model:

    public double ColumnWidth { get; set; } = double.NaN;
    

    If you set the ColumnWidth property dynamically, you should implement the INotifyPropertyChanged interface and raise the PropertyChanged event in the setter to notify the view.

    Since you can't style a GridViewColumn, using a DataTrigger is not an option.

    But you shouldn't really need one as you can set the source property directly in your view model and handle all the logic there.