简体   繁体   English

最大化窗口时WPF Datagrid不会填充整个高度和宽度

[英]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.当我在 WPF 应用程序中最大化窗口时,我的 DataGrid 没有占用整个空间。 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:这就是它在常规尺寸上的样子:

在此处输入图像描述

and this is how it looks like when window is max:这就是窗口最大时的样子:

在此处输入图像描述

What can I try next?接下来我可以尝试什么? I am new to WPF.我是 WPF 的新手。

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <Grid Grid.Row="0" Grid.Column="0">
            <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>

        <Grid Grid.Row="0" Grid.Column="1">
            <DataGrid x:Name="Grid" Padding="10 0 0 0" VerticalScrollBarVisibility="Visible" />
        </Grid>
    </Grid>

Firstly if you wanna make your datagrid fill a container automatically, you need to use Grid, not StackPanel also your datagrid sizes need to be set auto.首先,如果你想让你的数据网格自动填充容器,你需要使用网格,而不是 StackPanel,你的数据网格大小也需要设置为自动。

Edit: As @Erjon said : You don't have to use a container when you have a single DataGrid.But if you have more components with your DataGrid, Grid will be a better container choice instead of StackPanel.编辑:正如@Erjon 所说:当你有一个DataGrid 时,你不必使用容器。但是如果你的DataGrid 有更多组件,那么Grid 将是一个更好的容器选择,而不是StackPanel。

Your main GridDefination sizes were set as Auto, that was wrong.You need to work with "*" if you want a resposive design.Auto means that "Set this control's size with its children".您的主要 GridDefination 大小设置为 Auto,这是错误的。如果您想要响应式设计,您需要使用“*”。Auto 表示“设置此控件及其子控件的大小”。

在此处输入图像描述

Try adding in the datagrid ColumnWidth="*"尝试在数据网格中添加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="*" />第一个网格的第二个ColumnDefinition应该是<ColumnDefinition Width="*" />

And as @Zacos said in my answers comment you have to remove Width正如@Zacos 在我的回答评论中所说,您必须删除 Width

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM