I have the following window setup:
<Window x:Class="SearchWindow"
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:POR"
mc:Ignorable="d"
Title="SearchWindow"
Height="450"
Width="1000"
Loaded="Window_Loaded"
ResizeMode="NoResize"
Icon="/Images/receipt.ico"
WindowStartupLocation="CenterScreen">
<Grid>
<StackPanel Orientation="Vertical">
<!-- Search Box -->
<StackPanel x:Name="SP_SearchBox" Margin="0,5,0,0"
DockPanel.Dock="Top" Orientation="Horizontal" HorizontalAlignment="Center">
<Border CornerRadius="6"
BorderBrush="Gray"
BorderThickness="2"
Padding="1">
<StackPanel Orientation="Horizontal">
<Image Source="\Images\magnify.png" Height="21" VerticalAlignment="Center"/>
<TextBox x:Name="TB_search"
Width="220"
FontSize="12"
BorderThickness="0"
VerticalAlignment="Center"
HorizontalContentAlignment="left"
Padding="0,0,0,0"
Text="Search..."
Background="Transparent"
Foreground="Gray"
TextChanged="TB_Search_TextChanged"
PreviewKeyUp="TB_search_PreviewKeyUp"/>
<Image x:Name="BTN_Clear" Source="\Images\close.png" Height="20"
VerticalAlignment="Center" Visibility="Visible" Cursor="Hand"
MouseLeftButtonUp="BTN_Clear_MouseLeftButtonUp"/>
</StackPanel>
</Border>
</StackPanel>
<!-- Auto Suggestion box -->
<Popup x:Name="Popup" IsOpen="False" PlacementTarget="{Binding ElementName=SP_SearchBox}"
AllowsTransparency="True" PopupAnimation="Slide" Width="263" HorizontalOffset="-2" VerticalOffset="-2">
<Border CornerRadius="6"
BorderBrush="Gray"
BorderThickness="1"
Padding="1">
<ListView x:Name="LV_hints" Visibility="Visible" BorderThickness="0"
SelectionChanged="LV_hints_SelectionChanged"/>
</Border>
</Popup>
<!-- Grid 1 -->
<StackPanel Orientation="Vertical">
<DataGrid x:Name="DG_Orders" ItemsSource="{Binding}"
Margin="6" VerticalContentAlignment="Center" Visibility="Hidden">
<DataGrid.Columns>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Click="BTN_ShowItems_Click"
Margin="3">Show Items</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
<!-- Grid 2 -->
<DataGrid x:Name="DG_OrderItems" ItemsSource="{Binding}"
Margin="6" VerticalContentAlignment="Center" Visibility="Hidden">
<DataGrid.Columns>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button x:Name="BTN_Receipt" Click="BTN_Receipt_Click"
Margin="3">Receipt</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</StackPanel>
</StackPanel>
</Grid>
I cannot figure out why am I not getting a scroll bar when the bottom DataGrid has more data and appears off the window at the bottom of it.
I've tried using <DockPanel> in various places, like the top-most <StackPanel> but that still did not show me any scroll bars. I have read that using controls such as <DockPanel> or <StackPanel> should automatically give me a scrollbar when the content does not fit on the window, but in my case it just goes off screen.
I have also tried using <Grid.ColumnDefinitions> and <Grid.RowDefinitions> and having a row and a column on the sides as well as top and bottom for 5px margin for the content, but again, the bottom DataGrid just expands down and cuts off on the window.
Thanks in advance!
The
DataGridneeds a height constraint otherwise it will stretch to infinite. If the layout is defined correctly, theDataGridwill provide its ownScrollViewer(same applies to other ItemsControl classes likeListBoxandListView).Panels in general, like
Grid,DockPanel,CanvasorStackPanel, don't have their ownScrollViewer. They need to be hosted by aScrollViewerexplicitly.You can define this constraint either by hosting it inside a
Gridor by explicitly setting theHeightproperty.For complex layout arrangements and advanced resize behavior, you should always prefer to use a
Gridas root panel. At least you should know the different resize behavior. In your case, theStackPanelis not the proper root panel to layout the page.The following example creates a similar layout by utilizing the
Gridas a content host. Instead of a vertically orientedStackPanelit uses aGridthat defines rows.The example shows how to provide a height constraint by setting an explicit height for the
DataGrid(for the firstDataGrid) and by using the implicit height that is calculated by the parentGrid(for the secondDataGrid).The first
DataGridis located in the second row. The height constraint is locally set by settingDataGrid.Height.The second
DataGridis located in the third row, which is set to*: it will stretch to occupy the remaining height of the parent (theGrid, whereGrid.Heightis dictated byWindow.Height).Because the
Heightreturned by the third row evaluates to an actual value, the containedDataGrid.Heightwill automatically adapt to this value.For both
DataGridelements, due to the height constraint, the overflowing of rows will result in the scroll bars being displayed.