Erik Mandke
Erik Mandke

Reputation: 1667

WPF Datagrid - Not showing any Scrollbar

My Datagrid has a binding on an ObservableCollection and gets filled after grouping some values fetched by EF.

My Problem is, that the datagrid-height grows beyond the window size. Does anyone know how to get that fixed... I almost googled myself to death.. :o

<UserControl x:Class="UltranizerV2.Views.Storage.InventoryList"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d">
    <Grid>
        <Grid.RowDefinitions>

            <RowDefinition Height="*"></RowDefinition>
            <RowDefinition Height="25"></RowDefinition>
        </Grid.RowDefinitions>


        <DockPanel  Grid.Row="0" >
            <DataGrid ItemsSource="{Binding PresentableInventoryItems}" VerticalAlignment="Stretch" AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Produkttitel" Width="350" Binding="{ Binding ProductTitle}"/>
                <DataGridTextColumn Header="Sku" Width="100" Binding="{ Binding Sku}" />
                <DataGridTextColumn Header="Menge" Width="60"  Binding="{ Binding Quantity}" />
            </DataGrid.Columns>    
            </DataGrid>
        </DockPanel>
        <Label Grid.Row="1">Arsch</Label>
        </Grid>

</UserControl>

Upvotes: 22

Views: 35551

Answers (5)

JAVIER ESCUDERO
JAVIER ESCUDERO

Reputation: 11

Put

<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>

It worked for me.

Upvotes: 0

NoWar
NoWar

Reputation: 37632

Here is the best answer and I will tell you why.

  1. You have to use special properties of DataGrid are ScrollViewer
ScrollViewer.CanContentScroll="True" 
ScrollViewer.VerticalScrollBarVisibility="Auto"
ScrollViewer.HorizontalScrollBarVisibility="Auto"
  1. You have to adjust Height dinamically to parent of the DataGrid.

    Height="{Binding ElementName=parentElementName, Path=ActualHeight}"

This is working example of my code.

 <Grid x:Name="gridClientsContainer">
     <DataGrid  x:Name="dgClients"  IsReadOnly="True" CanUserAddRows="False" SelectionMode="Single"    VerticalAlignment="Top"        Margin="0 0 0 0" AutoGenerateColumns="False" ItemsSource="{Binding ClientItems}"  RowHeaderWidth="0"    ScrollViewer.CanContentScroll="True" 
      ScrollViewer.VerticalScrollBarVisibility="Auto" 
ScrollViewer.HorizontalScrollBarVisibility="Auto" Height="{Binding ElementName=gridClientsContainer, Path=ActualHeight}">
       <DataGrid.Columns>
          <DataGridTextColumn Binding="{Binding LastName}" Header="Last Name" MinWidth="120" />
          <DataGridTextColumn Binding="{Binding FirstName}" Header="First Name" MinWidth="100" />
     </DataGrid.Columns>
  </DataGrid>
</Grid>

Upvotes: 6

Dhru &#39;soni
Dhru &#39;soni

Reputation: 1058

Set The Property in datagrid

<DataGrid AutoGenerateColumns="False" Grid.Column="0" Grid.Row="0"
      ScrollViewer.CanContentScroll="True" 
      ScrollViewer.VerticalScrollBarVisibility="Auto"
      ScrollViewer.HorizontalScrollBarVisibility="Auto">
</DataGrid>

Upvotes: 7

Dhaval Patel
Dhaval Patel

Reputation: 7601

you can use the scrollviewer like

  <ScrollViewer ScrollViewer.VerticalScrollBarVisibility="Auto">
    <DataGrid ItemsSource="{Binding PresentableInventoryItems}" VerticalAlignment="Stretch" AutoGenerateColumns="False" Height="500">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Produkttitel" Width="350" Binding="{ Binding ProductTitle}"/>
                <DataGridTextColumn Header="Sku" Width="100" Binding="{ Binding Sku}" />
                <DataGridTextColumn Header="Menge" Width="60"  Binding="{ Binding Quantity}" />
            </DataGrid.Columns>    
            </DataGrid>
</ScrollViewer>

If I have define the height of Datagrid the Scrollbar visible.

enter image description here

Upvotes: 7

dkozl
dkozl

Reputation: 33384

To sum up comments your control looks fine which would suggest that the problem is somewhere up the visual tree. Most likely InventoryList, or one of its parents, it's placed in control that gives its children infinite amount of space to grow like StackPanel, ScrollViewer or Canvas. Because of that DataGrid can grow to accommodate all item hence not scroll bar is visible.

Remove that control or replace it with one that limits the size of its children

Upvotes: 36

Related Questions