wpfwpfdatagriddatagridcolumnheader

Dynamically Show/Hide WPF DataGrid Header Column


I am using VS 2010. I want to display Datagrid header only on grid mouse over. I wrote following code, but it is not working.

<StackPanel>
  <DataGrid  ItemsSource="{Binding SelectedItemsCollectionView}"
      AutoGenerateColumns="False" CellStyle="{StaticResource CellStyle}" 
      GridLinesVisibility="None"  RowHeight="28" HeadersVisibility="None"  
      ClipToBounds="True" AllowDrop="True" >
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Path=DisplaySequence}"/>
        </DataGrid.Columns>
        <DataGrid.Style>
            <Style TargetType="DataGrid">
                <Setter Property="HeadersVisibility" Value="None" />
                <Style.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="HeadersVisibility" Value="Column" />
                    </Trigger>
                </Style.Triggers>
            </Style>
        </DataGrid.Style>
    </DataGrid>
</StackPanel>

Any suggestions?

Thanks!


Solution

  • Just Remove HeadersVisibility="None" in DataGrid tag. Its working fine.

    HeadersVisibility is a dependency property.They have Value resolution strategy. Local value has more precedence compare to the trigger value.

    enter image description here

    Source. For detail see here