0
votes

I need some help because I'm stuck with the horizontal scrollbar in the datagrid.

I have a TabControl and I've add a DataGrid to a TabItem, the problem is the Horizontal ScrollBar is never visible, even if I set ScrollViewer.HorizontalScrollBarVisibility="Visible" or if I put the DataGrid in a Grid

             <TabControl Margin="6,0,6,0" >
                  <TabItem>
                      <DataGrid  ItemsSource="{Binding}" AutoGenerateColumns="False" IsReadOnly="True" HorizontalAlignment="Center" VerticalAlignment="Center" SelectionMode="Extended">
                          <DataGrid.Columns>
                             <DataGridTextColumn Binding="{Binding BlaBla}" Header="BlaBlaBla" />
        ...
                         </DataGrid.Columns>
                     </DataGrid>
                  </TabItem>
...

Can someone help me to point the problem? Thanks.

Edit :

Here is a sample code that reproduce the same problem, the vertical scrollbar is showing correctly but not the horizontal scrollbar

Xaml:

<Window x:Class="BlaBla.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="300">
    <Grid>
        <DataGrid  Name="datagrid" ItemsSource="{Binding}" AutoGenerateColumns="False" IsReadOnly="True" HorizontalAlignment="Center" VerticalAlignment="Center" >
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding Date}" Header="Date" />
                <DataGridTextColumn Binding="{Binding Path=User}" Header="User"/>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

Code-behind :

    datagrid.DataContext = this.SomeDataTable;

It doesn't work if I do <ScrollViewer HorizontalScrollBarVisibility="Visible"> around the controls

2
For troubleshooting sake, if you set a fixed Width/Height on your DataGrid that is less than the contents require, does it invoke the Scrollbar then? - Chris W.
Thanks for you quickly answer, I've tried this tips but the scrollbar doesn't invoke. - Christopher Sauer
If you do a <ScrollViewer HorizontalScrollBarVisibility="Visible" Width="50"> around the controls do you get a scrollbar?.. Oh also, set the width of the inner control to like 200 for this test. - safetyOtter
Posted code works fine. You need to add small sample code here to replicate an issue so that anyone can help you out. - Rohit Vats

2 Answers

1
votes

Thats very simple, Add a ScrollViewer and set Auto forHorizontalScrollBarVisibility. try this.

<Grid>
    <TabControl>
        <TabItem Header="Hello">

                <ScrollViewer HorizontalScrollBarVisibility="Auto">
                    <DataGrid >
                        <DataGrid.Columns>
                            <DataGridTextColumn Header="Name" />
                            <DataGridTextColumn Header="Name" />
                            <DataGridTextColumn Header="Name" />
                            <DataGridTextColumn Header="Name" />
                            <DataGridTextColumn Header="Name" />
                            <DataGridTextColumn Header="Name" />
                            <DataGridTextColumn Header="Name" />
                            <DataGridTextColumn Header="Name" />
                            <DataGridTextColumn Header="Name" />
                            <DataGridTextColumn Header="Name" />
                            <DataGridTextColumn Header="Name" />
                            <DataGridTextColumn Header="Name" />
                            <DataGridTextColumn Header="Name" />
                            <DataGridTextColumn Header="Name" />
                        </DataGrid.Columns>

                    </DataGrid>
                </ScrollViewer>

        </TabItem>
        <TabItem Header="Hello2">
        </TabItem>
        <TabItem Header="Hello3">
        </TabItem>
    </TabControl>
</Grid>
0
votes

Ok, I resolve the issue but I don't know why it has its behavior. I had to put the scroll viewer's style on the resource of the parent window, otherwise it doesn't show the horizontal scrollbar but only the vertical scroll bar.

If anyone know why I'll be glad to know the explanation.