0

I am trying to collapse a column according to a condition with this code:

<DataGridTextColumn Header="MyColumn" Binding="{Binding MyProperty}">
    <DataGridTextColumn.HeaderStyle>
        <Style TargetType="{x:Type DataGridColumnHeader}">
            <Setter Property="Width" Value="0cm"/>
            <Setter Property="Visibility" Value="Collapsed"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=ThisView, Path=DataContext.MyboolProperty}" Value="true">
                    <Setter Property="Width" Value="2.5cm"/>
                    <Setter Property="Visibility" Value="Visible"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </DataGridTextColumn.HeaderStyle>
</DataGridTextColumn>

The problem is that the column is not complete collapsed, I can see a part of the column.

I have tried this code too:

<DataGridTextColumn Header="MyColumn" Binding="{Binding MyProperty}"
        Width="0cm"
        Visibility="Collapsed">
    <DataGridTextColumn.HeaderStyle>
        <Style TargetType="{x:Type DataGridColumnHeader}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=ThisView, Path=DataContext.MyboolProperty}" Value="true">
                    <Setter Property="Width" Value="2.5cm"/>
                    <Setter Property="Visibility" Value="Visible"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </DataGridTextColumn.HeaderStyle>
</DataGridTextColumn>

In this case the column is collapsed as expected, but then if the property if the trigger is true, the column is still collapsed.

Also I have tried this option:

<DataGridTextColumn Header="MyColumn" Binding="{Binding MyProperty}">
    <DataGridTextColumn.HeaderStyle>
        <Style TargetType="{x:Type DataGridColumnHeader}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=ThisView, Path=DataContext.MyboolProperty}" Value="true">
                    <Setter Property="Width" Value="2.5cm"/>
                    <Setter Property="Visibility" Value="Visible"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding ElementName=ThisView, Path=DataContext.MyboolProperty}" Value="false">
                    <Setter Property="Width" Value="0cm"/>
                    <Setter Property="Visibility" Value="Collapsed"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </DataGridTextColumn.HeaderStyle>
</DataGridTextColumn>

But the behavior is the same than first option, it is not full collapsed but it works when the property of the trigger is true.

Álvaro García
  • 18,114
  • 30
  • 102
  • 193
  • The data trigger is changing visibility of column header, not column (as in second example). – Sinatr Jan 25 '19 at 12:31

1 Answers1

1

Your data trigger is located inside column header template, therefore it can only change properties of column header (not a complete column, i.e. cells).

There is no Style for DataGridTextColumn, so you can't use DataTrigger. But converter should do.

However, another problem is that columns aren't in visual tree, so you can't use ElementName or RelativeSource, they simply won't work. There is an easy workaround however. Then your column will look like this:

<Window.Resources>
    <BooleanToVisibilityConverter x:Key="converter" />
    <local:BindingProxy x:Key="proxy" Data="{Binding}" />
</Window.Resources>

...

<DataGridTextColumn Header="MyColumn"
                    Binding="{Binding MyProperty}"
                    Visibility="{Binding Data.MyboolProperty, Source={StaticResource proxy}, Converter={StaticResource converter}}" />
Sinatr
  • 20,892
  • 15
  • 90
  • 319