Search code examples
wpf

WPF Datagrid does not fill the entire height and width when maximizing window


My DataGrid is not taking the whole space when I maximize the window in my WPF application. This is how I created the layout:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="Auto" />
    </Grid.ColumnDefinitions>
    <Grid Grid.Row="0" Grid.Column="0" Width="265">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="200" />
            <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>

        <Label Grid.Row="0" Grid.Column="0" Content="Date From:" />
        <Label Grid.Row="1" Grid.Column="0" Content="Date To:" />
        <DatePicker Grid.Column="1" Grid.Row="0" Margin="3" x:Name="DateFrom" />
        <DatePicker Grid.Column="1" Grid.Row="1" Margin="3" x:Name="DateTo" />
        <Button Grid.Column="1" Grid.Row="2" HorizontalAlignment="Right" 
                        MinWidth="80" Margin="3" Content="Send" Click="PopulateGrid"  x:Name="BtnPopulateGrid"/>
    </Grid>

    <StackPanel Grid.Row="0" Grid.Column="1">
        <DataGrid Width="Auto" x:Name="Grid" Height="553" 
                  Padding="10 0 0 0" VerticalScrollBarVisibility="Visible" Margin="10,0,-707,0" />
    </StackPanel>
</Grid>

and this is how it looks like on regular size:

enter image description here

and this is how it looks like when window is max:

enter image description here

What can I try next? I am new to WPF.


Solution

  • Try adding in the datagrid ColumnWidth="*"

    It will expand all columns to avaiable space and the datagrid will fill it's parent

    The second ColumnDefinition of the first grid should be <ColumnDefinition Width="*" />

    And as @Zacos said in my answers comment you have to remove Width