Reputation: 49
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.
Upvotes: 1
Views: 8540
Reputation: 881
Wrapping the DataGrid in a Grid, DockPanel, ContentControl or directly in the Window should solve the problem. Also set ScrollViewer.VerticalScrollBarVisibility="Auto" and ScrollViewer.CanContentScroll="True" like the following XAML code:
<Grid Grid.Row="1" MinHeight="300">
<DataGrid
ScrollViewer.VerticalScrollBarVisibility="Auto"
ScrollViewer.CanContentScroll="True">
<DataGrid.Columns>
<DataGridTextColumn />
<DataGridTextColumn />
<DataGridTextColumn />
</DataGrid.Columns>
</DataGrid>
</Grid>
You should set whatever MinHeight that is appropriate in your case.
*) Do not wrap DataGrid in a StackPanel
Upvotes: 0
Reputation: 361
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.
Upvotes: 0
Reputation: 169370
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.
Upvotes: 4