Technetium Tc
Technetium Tc

Reputation: 21

How can I automatically resize objects that are in a grid?

So I have recently started learning more C# and I am currently trying to create a GUI. I so far have a title and a text box that looks like this:
https://i.sstatic.net/SIwVU.png

Yes, this looks fine, it's not the problem. The problem is when you open the window (by debugging it) the window is set to the size of which it looks like on the preview. Which is fine. But, when I maximize it the objects inside of the window do not get larger. They stay at the same size. Which looks like this:
BEFORE:
https://i.sstatic.net/lFNU3.png
AFTER:
https://i.sstatic.net/7FdYk.png

How can I make it so the objects inside of the window get larger just like the window itself does?

MainWindow.xaml:

<Window x:Name="THE_GUI" x:Class="MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:GUI"
        mc:Ignorable="d"
        Title="GUI" Height="450" Width="800" Foreground="Black" WindowStyle="SingleBorderWindow">
    <Grid>
        <TextBlock x:Name="GUI_TITLE2" HorizontalAlignment="Center" Margin="0,10,0,0" Text="TITLE" TextWrapping="Wrap" VerticalAlignment="Top" Height="50" Width="125" FontSize="18"/>
        <TextBox HorizontalAlignment="Left" Margin="41,35,0,0" Text="TextBox" TextWrapping="Wrap" VerticalAlignment="Top" Width="204" Height="375" IsReadOnly="True"/>
    </Grid>
</Window>


If you require more code I can provide it
Thanks! :) PS I did see this question: WPF: How can I have controls in a grid automatically resize when the grid is resized? but it doesn't help me at all.

Upvotes: 0

Views: 173

Answers (2)

Shweta Lodha
Shweta Lodha

Reputation: 98

As suggested by Prateek also, simply putting controls under grid won't work. If you want to go with grid control, then please have row and columns created to align the controls. I've updated your code and here it is:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="13*"/>
        <RowDefinition Height="87*"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="50*"/>
        <ColumnDefinition Width="20*"/>
        <ColumnDefinition Width="30*"/>
    </Grid.ColumnDefinitions>
    <TextBlock Grid.Row="0" Grid.Column="1"
        x:Name="GUI_TITLE2" HorizontalAlignment="Stretch" 
               Text="TITLE" TextWrapping="Wrap" VerticalAlignment="Stretch" 
               FontSize="18"/>
    <TextBox  Grid.Row="1" Grid.Column="0"
        HorizontalAlignment="Stretch" Text="TextBox" TextWrapping="Wrap"
             VerticalAlignment="Stretch" IsReadOnly="True"/>
</Grid>

Upvotes: 0

Prateek Shrivastava
Prateek Shrivastava

Reputation: 1937

Do not set Height & Width of controls (TextBlock/TextBox) if you want them to resize. Also you are using a plain grid with no Row/Columns so WPF will not know how to scale your GUI.

You need to read about different type of Panels/Containers available in WPF and how to use them to Lay your Controls.

Check: StackPanel, WrapPanel, DockPanel. For Grid read about RowDefinitions and Columndefinitions. Read the WildCard height and width assignment.

Upvotes: 1

Related Questions