WPF datagrid shows horizontal scroll bar when the actual width of datagrid equals to the sum of actual width of all columns' actual width

53 views Asked by At

I run into the problem as the title says. I have datagrid inside a scroll viewer in a wpf window. I tried using tools like Snoop to see the actual width of the datagrid and the actual width of each column. I set the width of each column to "*" and HorizontalScrollBarVisibility property to Auto in xaml. My xaml code is

<ScrollViewer
    x:Name="scrollViewer"
    HorizontalScrollBarVisibility="Auto"
    VerticalScrollBarVisibility="Disabled">
    <DataGrid
        Name="dataGrid"
        Width="{Binding ElementName=scrollViewer, Path=ActualWidth}"
        Height="{Binding ElementName=scrollViewer, Path=ActualHeight}"
        HorizontalAlignment="Stretch"
        VerticalAlignment="Stretch"
        Background="White"
        CanUserAddRows="False"
        ColumnHeaderHeight="32"
        ColumnWidth="*"
        EnableColumnVirtualization="True"
        EnableRowVirtualization="True"
        MinColumnWidth="120"
        RowHeight="30"
        SelectionUnit="Cell"
        UseLayoutRounding="False"
        VirtualizingPanel.IsVirtualizingWhenGrouping="True"
        VirtualizingStackPanel.IsVirtualizing="True"
        VirtualizingStackPanel.VirtualizationMode="Recycling" />
</ScrollViewer>

Here are width data I got from Snoop. The actual width of scroll viewer. The actual width of scroll viwer

The actual width of datagrid. The actual width of DataGrid

I have 5 columns

The actual width of each column

Actually, the actual width of scrollviewer equals to the actual width of datagrid. But it still shows the horizontal scroll bar. The strange thing is when I drag the scroll bar, it disappers.

After I removed the scroll viewer, I found that the horizontal scroll bar still exists. So the horizontal bar is belongs to the datagrid. As the picture above showed, The sum of the actual width of 5 columns, which is 5 * 204.8 = 1024, is equal to the actual width of the datagrid.

Could someone explain this to me?

0

There are 0 answers