Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a full size WPF grid [duplicate]

Tags:

c#

.net

wpf

I am trying to fit a wpf grid having 4 cells to be all the time full sized on the screen, having the content of the cells equally split, but i am having problems doing it... This is the code:

    <StackPanel x:Name="MainStackPanel" HorizontalAlignment="Center"  Orientation="Vertical">
    <StackPanel.Resources>
        <Style TargetType="Rectangle">
            <Setter Property="HorizontalAlignment" Value="Stretch"/>
            <Setter Property="Height" Value="Auto"/>
        </Style>
    </StackPanel.Resources>
    <Grid x:Name="Control1" HorizontalAlignment="Center" Height="150">
        <Grid.Resources>
            <Style TargetType="Rectangle">
                <Setter Property="HorizontalAlignment" Value="Stretch"/>
                <Setter Property="Height" Value="Auto"/>
            </Style>
        </Grid.Resources>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <Rectangle Fill="Red" Grid.Row="0" Grid.Column="1"/>
        <Rectangle Fill="Blue" Grid.Row="0" Grid.Column="2"/>
        <Rectangle Fill="Green" Grid.Row="1" Grid.Column="2"/>
        <Rectangle Fill="Yellow" Grid.Row="1" Grid.Column="1"/>
    </Grid>
</StackPanel>

Please let me know how should i get this working... or what i am doing wrong...

like image 242
Clock Avatar asked Mar 22 '26 05:03

Clock


1 Answers

If you want to have a grid that has four equally spaced cells then you could do something like this.

 <Grid.ColumnDefinitions>
        <ColumnDefinition Width="1*"/>
        <ColumnDefinition Width="1*"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="1*"/>
        <RowDefinition Height="1*"/>
    </Grid.RowDefinitions>

This will create a 2x2 grid that will automatically resize if the screen is resized. In your example, you have your grid inside of a stack panel so it is only going to fill the size of the stack panel. If you want a grid for the entire screen, you need to put your grid as your first container and set its constraints as shown above.

like image 197
ProgrammingDude Avatar answered Mar 23 '26 20:03

ProgrammingDude