Binding Visibility for DataGridColumn in WPF

Dec 20, 2016 12:40 · 81 words · 1 minute read

First you need a BooleanToVisibilityConverter to Converter bool to Visibility

MaterialDesignThemes has already owned one

add follow code to your xaml file

<converters:BooleanToVisibilityConverter x:Key="booleanToVisibilityConverter" />

refer to

DataGridTextColumn or any other supported dataGrid columns doesn’t lie in Visual tree of DataGrid, so the solution is:

we add

<FrameworkElement x:Name="dummyElement" Visibility="Collapsed" />

to UserControl under Grid

just like this

        <FrameworkElement x:Name="dummyElement" Visibility="Collapsed" />

then use it

<DataGridTemplateColumn Width="Auto"                                                  Header="MyColumn"                                                 Visibility="{Binding DataContext.isVisible,                                                                   Converter={StaticResource booleanToVisibilityConverter},                                                                      Source={x:Reference dummyElement}}">