0

Good day,

I have a WPF DataGrid showing the content of a large DataTable. This table is much larger than the screen so the user interacting with this table needs scrollbars to be able to see all columns and rows. As you can see the picture 1 the vertical scrollbar is visible but disabled while the horizontal scrollbar is not visible at all.

How can I make the scrollbars work?

The DataGrid is filled via data binding to a DataTable after some user interaction:

this.topPhrases.DataContext = loadedValues.DefaultView;

where topPhrases is a DataGrind and laodedValues a DataTable

This is the xaml code of the DataGrid:

    <DataGrid Name="topPhrases" Grid.Row="1"  Margin="10,0"  VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Visible" ItemsSource="{Binding}">
    </DataGrid>

The hierarchy of the GUI elements are: Window->Grid->WrapPanel->ContentControl->Grid->DataGrid

I tried many things I found in the internet like: how can I enable scrollbars on the WPF Datagrid? but no suggestions worked so far.

Community
  • 1
  • 1
Moritz Nadler
  • 39
  • 1
  • 7
  • See how it works without that odd layout hierarchy, e.g. put the DataGrid directly into the top level Grid. – Clemens Jan 12 '17 at 10:03
  • What's the size of the `Grid.Row`? Scroll will be visible only if your `DataGrid` is smaller height than the parent control, in this case `Grid.Row`. It is possible you set for example ``. Your `DataGrid` could be bigger than that, and now you just dont see entire `DataGrid`. Try put `DataGrid.Height="50"` or something small. See what happens then. – daidai Jan 12 '17 at 10:23
  • @daidai Setting a fixed Height into the RowDefinition make both the horizontal and vertical scrollbars work. But what I really want is the DataGrid not to have a fixed size but the size of the main window (minus the height of a toolbar at the top of course) – Moritz Nadler Jan 12 '17 at 10:40

2 Answers2

4

Setting a fixed Height into the RowDefinition make both the horizontal and vertical scrollbars work. But what I really want is the DataGrid not to have a fixed size but the size of the main window (minus the height of a toolbar at the top of course)

Set the Height of the first RowDefinition to Auto and the second to *, e.g.:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication1"
        mc:Ignorable="d"
        Title="MainWindow" Height="300" Width="300">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <ToolBar />
        <DataGrid Grid.Row="1" />
    </Grid>
</Window>

Also make sure that you don't use any StackPanels. Please refer to my answer here for more information why:

Horizontal scroll for stackpanel doesn't work

If you need any further help on this I suggest that you post the entire XAML markup of your window.

Community
  • 1
  • 1
mm8
  • 163,881
  • 10
  • 57
  • 88
  • Thanks for your answer.I tried your suggestion with Auto and * but it leads to same behaviour as I had so far. Maybe I also need to mention that I don't want to have a fixed size for the main window. E.g.: If the user makes it full screen the table should increase in size – Moritz Nadler Jan 12 '17 at 12:01
  • Try to remove the WrapPanel and perhaps also the ContentControl. And again, please provide a complete reproducible sample of your issue if you need any further help. – mm8 Jan 12 '17 at 13:19
0

WPF. In my case, I had DataGrid that failed to vertically scroll. The DataGrid was wrapped in a StackPanel. To get it to work, I had to wrap the StackPanel with

<ScrollViewer VerticalScrollBarVisibility="Auto"> 

and the closing

</ScrollViewer> 

at the end.

Eric Wood
  • 331
  • 1
  • 3
  • 14