WPF Datagrid - Don't show scrollbar

My Datagrid is bound to an ObservableCollection and is populated after grouping some values ​​received by EF.

My problem is that the height of the datagrid exceeds the size of the window. Does anyone know how to fix this ... I almost guessed 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> 
+13
c # wpf datagrid scrollbar
Jun 03 '14 at 13:00
source share
4 answers

To summarize the comments, your control looks great, which indicates that the problem is somewhere up the visual tree. Most likely, InventoryList or one of his parents, he put in control, which gives his children an infinite amount of space to grow like a StackPanel , ScrollViewer or Canvas . Because of this, the DataGrid can grow to accommodate the entire element, so the scroll bar is not displayed.

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

+18
Jun 03 '14 at 13:31 on
source share

you can use 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 determine the height of the Datagrid, then the visible scrollbar.

enter image description here

+4
Jun 03 '14 at
source share

Set property in datagrid

 <DataGrid AutoGenerateColumns="False" Grid.Column="0" Grid.Row="0" ScrollViewer.CanContentScroll="True" ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollBarVisibility="Auto"> </DataGrid> 
+3
Mar 24 '15 at 7:14
source share

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

  • You must use the special properties of the DataGrid: ScrollViewer
 ScrollViewer.CanContentScroll="True" ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollBarVisibility="Auto" 
  1. You must configure the dynamic dynamics of the Height for the DataGrid parent.

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

This is a 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> 
+2
Nov 03 '17 at 3:09 on
source share



All Articles