WPF DataGrid height increases when adding data

I have a DataGrid WPF that grows in height when I add data to it that won't fit in its initial height. I do not want the height to change unless the user has increased the size of the window. Is there any way to stop this auto resizing?

<Window x:Class="WpfDataGridSizeTest.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Loaded="Window_Loaded" SizeToContent="WidthAndHeight"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="*"/> </Grid.RowDefinitions> <DockPanel Grid.Row="0"> <DataGrid x:Name="wordsDataGrid" VerticalAlignment="Top" ItemsSource="{Binding}" MinHeight="100" SelectionMode="Single" AutoGenerateColumns="False" VerticalScrollBarVisibility="Auto" > <DataGrid.Columns> <DataGridTextColumn Header="Column" Width="Auto" Binding="{Binding AString}"/> </DataGrid.Columns> </DataGrid> </DockPanel> </Grid> </Window> public partial class MainWindow : Window { MyList myList = new MyList(); public MainWindow() { InitializeComponent(); } private void Window_Loaded(object sender, RoutedEventArgs e) { wordsDataGrid.DataContext = myList; for (int i = 0; i < 100; ++i) { myList.AddOne("blah blah"); } } } public class MyList : ObservableCollection<AClass> { public MyList() { } public void AddOne(string aString) { base.Add(new AClass(aString)); } } public class AClass { public string AString { get; set; } public AClass(string aString) { AString = aString; } } 
+6
source share
6 answers

If I'm not mistaken, you want your DataGrid to be in centain Height initally, with some empty space under the DataGrid on Windows ... Then, when you resize the window, the DataGrid will resize it.

Add another row to the grid and define the MinHeight of this row. Then set the DataGrid as VerticalAlignment = Stretch. Also set the default size for the window.

 <Window x:Class="WpfDataGridSizeTest.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Loaded="Window_Loaded" Height="300"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="*"/> <RowDefinition MinHeight="100"/> </Grid.RowDefinitions> <DockPanel Grid.Row="0" VerticalAlignment="Stretch"> <DataGrid x:Name="wordsDataGrid" VerticalAlignment="Stretch" ItemsSource="{Binding}" MinHeight="100" SelectionMode="Single" AutoGenerateColumns="False" VerticalScrollBarVisibility="Auto" > <DataGrid.Columns> <DataGridTextColumn Header="Column" Width="Auto" Binding="{Binding AString}"/> </DataGrid.Columns> </DataGrid> </DockPanel> </Grid> </Window> 
+4
source

I had the same problem and fixed it by simply removing SizeToContent = "WidthAndHeight" in the window definition.

+2
source

This will do what you want:

 <Grid> <Grid.RowDefinitions> <RowDefinition Height="1*"/> <RowDefinition Height="1*"/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="70"/> <ColumnDefinition Width="Auto"/> </Grid.ColumnDefinitions> <DataGrid Name="dgMain" AutoGenerateColumns="True"></DataGrid> </Grid> 

together with that:

 public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); Foo foo; List<Foo> foos = new List<Foo>(); foo = new Foo() { Name = "Sjaak" }; foos.Add(foo); foo = new Foo() { Name = "Joepie" }; foos.Add(foo); dgMain.ItemsSource = foos; } } public class Foo { public Foo() { } public String Name { get; set; } } 

The focus is in the proportional (even) distribution of row heights, using "1 *" for the Height properties of both rows. You can also assign it to other "common" ones by setting one line "Height" to "2 *", etc.

+1
source

For example, try using VerticalAlignment = Top or explicitly set the Height property to some value.

It can also help put the DataGrid in the StackPanel and control the behavior of the StackPanel sizes.

0
source

If the problem is that the entire grid is growing (but you are happy with the growth of individual rows), then: what is the panel of the parent element in which the DataGrid is located? Changing this parameter may affect the size of the control.

For example: a StackPanel will allow its children to grow in one direction (stacking direction), and the DockPanel limit the size of the β€œfilled” item to the available space.

0
source
  VerticalAlignment="Bottom" 

worked for me to solve this same problem. I also used

  VerticalScrollBarVisibility="Auto" 
0
source

Source: https://habr.com/ru/post/917875/


All Articles