WPF Grid is not stretching vertically

故事扮演 提交于 2019-12-23 09:26:43

问题


I've the following Grid in the main window:

<Grid Name="gridMain" Width="Auto" VerticalAlignment="Stretch" Height="Auto">
</Grid>

I'm adding a UserControl dynamically in the grid. The main window only has this grid. I want the grid to be stretched both horizontally and vertically and should fill up entire window. The grid is stretching horizontally but doesn't stretch vertically. Any idea why? What do I need to do to make the grid stretch vertically?

EDIT

Here is the complete XAML.

<Window x:Class="Sensitech.TurboValidator.UserControls.ConveyorBelt"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="ConveyorBelt" Height="329" Width="714" xmlns:my="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit">
    <Grid Name="gridMain" Width="Auto" VerticalAlignment="Stretch" Height="Auto" Background="Cyan">

    </Grid>
</Window>

回答1:


Try setting row height to *

<Grid Name="gridMain" >    
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
</Grid>

Similarly define ColumnDefinition and set Width to *

Also make sure grid is the one not stretching not the user control. You can easily do that by assigning some background color to Grid.




回答2:


There is a SizeChanged event in xaml e.g. SizeChanged="WindowSizeChanged" Use this to resize your controls to the size you want based on the window size.

    private void WindowSizeChanged(object sender, SizeChangedEventArgs e)
    {
        Grid1.Height = this.ActualHeight - 40;
    }

"Would you jump from a plane if you were told your parachute was packed by the xaml designers?"



来源:https://stackoverflow.com/questions/8641456/wpf-grid-is-not-stretching-vertically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!