Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Height of WPF DataGrid increases when data added

I have a WPF DataGrid that increases in height when I add data to it that won't fit inside its initial height. I don't want the height to change unless the user increases the Window size. Is there a way to stop this auto-resize?

<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;
        }
    }
like image 454
Ian Avatar asked Jun 12 '12 10:06

Ian


3 Answers

If I don't get you wrong, you want your DataGrid to be in centain Height initally, with some Empty Space below the DataGrid within the Windows...Then when resize the Window, the DataGrid will changes it size.

Add one more Row to the Grid, and define a MinHeight of that Row. Then set DataGrid to be VerticalAlignment = Stretch. Also set a default height 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> 
like image 152
King Chan Avatar answered Oct 09 '22 04:10

King Chan


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

like image 25
mijikin Avatar answered Oct 09 '22 06:10

mijikin


This will do what you desire:

<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>

alongside with this:

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 trick is in the proportional (even) distribution of the row heights by using "1*" for the Height properties of both rows. You can also ditribute it in other "shares" by setting one row Height to "2*" etc.

like image 41
Bernoulli IT Avatar answered Oct 09 '22 05:10

Bernoulli IT