Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Datagrid horizontal scrolling with only header data

Tags:

c#

wpf

datagrid

I have a WPF Datagrid, at initial stage i will assign 100 column header to Datagrid, but I am not able to horizontal scroll it to view all column headers.

DataGrid does not have any rows, ItemSource is null. How do I achieve horizontal scrolling when I have only column headers (no rows).

I binded ItemSource to a DataTable which has only column header and no rows.

How can I scroll in this scenario.

like image 483
Kishor Avatar asked Jan 14 '23 21:01

Kishor


1 Answers

Here is a workaround for this issue that worked for me: just place the DataGrid into a ScrollViewer and make a DataTrigger to set the HorizontalScrollBarVisibility of the ScrollViewer to Visible if DataGrid has no items.

<ScrollViewer VerticalScrollBarVisibility="Disabled">
    <ScrollViewer.Style>
        <Style TargetType="ScrollViewer">
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=dataGrid, Path=HasItems}" Value="False">
                    <Setter Property="HorizontalScrollBarVisibility" Value="Visible"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </ScrollViewer.Style>
    <DataGrid Name="dataGrid"/>
</ScrollViewer>
like image 197
max Avatar answered Jan 22 '23 13:01

max