.net 如何使 wpf 数据网格填充所有可用空间并随窗口调整大小?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20625081/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 12:34:23  来源:igfitidea点击:

How to make a wpf datagrid fill all available space and resize with window?

.netwpfsilverlightwindows-phone-7

提问by user2330678

How to make a wpf datagrid fill all available space and resize with window? Mind that the datagrid is inside a grid.

如何使 wpf 数据网格填充所有可用空间并随窗口调整大小?请注意,数据网格位于网格内。

回答by Orion Edwards

You should be able to do it for a datagrid (or for any WPF control) by setting HorizontalAlignmentand VerticalAlignmentto Stretch

您应该能够通过设置HorizontalAlignmentVerticalAlignment来为数据网格(或任何 WPF 控件)执行此操作Stretch

If it's inside a Grid, you should be able to set something like this

如果它在网格内,你应该能够设置这样的东西

<Grid> <!-- parent grid or whatever -->
   <DataGrid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ... >
   </DataGrid>
</Grid>

回答by Vishwaram Sankaran

  1. Never set height or width to it or its parent control
  2. No need to worry on the horizontal and vertical alignment since by default Horizontal and vertical alignment are stretch. if above are proper things should work :)
  1. 切勿为其或其父控件设置高度或宽度
  2. 无需担心水平和垂直对齐,因为默认情况下水平和垂直对齐是拉伸的。如果以上是正确的事情应该工作:)

回答by Jaihind

Remove all height and width property of its parent control. remove horizontal and vertical property. Define row height as * of its parent grid.

删除其父控件的所有高度和宽度属性。删除水平和垂直属性。将行高定义为其父网格的 *。

<Grid> 
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
   <DataGrid Grid.Row="0" >
   </DataGrid>
</Grid>

回答by Terry Tyson

Set the width for the last column of the DataGrid to *.

将 DataGrid 最后一列的宽度设置为 *。

<DataGridTextColumn Header="Notes"
                    Binding="{Binding Notes}"
                    Width="*"/>

Note : This actually works on only the last column of the DataGrid

注意:这实际上只适用于 DataGrid 的最后一列

回答by Thiha

Insert DataGrid directly to the Grid without wrapping with a panel.

将 DataGrid 直接插入到 Grid 中,无需使用面板包装。

    <Grid>
       <DataGrid>
       </DataGrid>
    </Grid>