I want to scroll the datagrid
when it's length exceeds the stackpanel
, so I tried this:
<StackPanel Orientation="Horizontal">
<ScrollViewer VerticalScrollBarVisibility="Auto" CanContentScroll="True">
<DataGrid Name="dgConfig" VerticalAlignment="Stretch" AutoGenerateColumns="False">
<DataGrid.Columns>
...
</DataGrid.Columns>
</DataGrid>
</ScrollViewer>
</StackPanel>
But this doesn't work, I have searched on this web and failed to find any avaiable solutions. So how should I fix this? Thanks!
DockPanel instead of StackPanel works for me.
ScrollViewers
andStackPanels
don't work very well together since aStackPanel
measures its child elements with infinite horizontal space if itsOrientation
property is set toHorizontal
and infinite vertical space if it is set toVertical
.So you will either have to specify a height for the
StackPanel
:If you don't it will have an infinite height and that's why you see no scrollbars.
The other, and much better option, would be to get rid of the
StackPanel
and use anotherPanel
that doesn't measures its child elements with an infinite space.The
DataGrid
has its ownScrollViewer
built-in, so you don't need to put it inside aScrollViewer
element yourself. Get rid of theStackPanel
(s) and theScrollViewer
:try adding VerticalScrollBarVisibility="Auto", ScrollViewer.CanContentScroll="True" to datagrid property.