简体   繁体   中英

Horizontal ScrollBar is never visible

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

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>

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM