NotVeryGoodAtThis
NotVeryGoodAtThis

Reputation: 33

WPF not formatting button and text box normally

I'm working on a WPF app, following a tutorial, and I get completely different results than his. I triple-checked my code to see if it's exactly the same as the tutorial guy's(it was), and my button just won't format correctly with the text box next to it. I expect the text box to take up 75% of the bottom of the screen, with the button taking up about 25%. Instead, the button takes up 99%, and the textbox takes up 1%. The tutorial guy's text box and button format perfectly. I'm linking the tutorial and throwing in my code below. For reference, the guy's using Visual Studio while I'm using JetBrains Rider. Does anyone know what might be causing the issue?

Link: https://youtu.be/FYB84gKCWAc?list=PL3g0PHY_11p4PD7Dt4X9KCLpOiyCsW8UD&t=262

<Window x:Class="Chatbot.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:Chatbot"
    mc:Ignorable="d"
    Title="Chatbot" Height="350" Width="525">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    
    <TextBox Grid.Row="0" Name="_tb_alpha" IsReadOnly="True"/>
    
    <Grid Grid.Row="1">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="2"/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        
        <TextBox Grid.Column="0" Name="_tb" MaxLength="50" />
        <Button Grid.Column="1" Name="_btn" Content="Send" Click="_btn_OnClick"/>
    </Grid>
</Grid>    

enter image description here

Upvotes: 0

Views: 89

Answers (2)

Shrimperator
Shrimperator

Reputation: 467

This is the cause of your problem:

<ColumnDefinition Width="2"/>

The video uses star sizing, you just forgot the asterisk '*'

<ColumnDefinition Width="2*"/>

Star sizing is a system in some WPF panels (such as grids), to let things take up a percentage of the full width. By default, the width would be "1*", so 2* is twice that -> resulting in a size of 75% for the textbox and 25% for the button (the button's column has the default width of 1*).

Currently, the column with your textbox in it is just set to 2 pixels.

Upvotes: 1

It all makes cents
It all makes cents

Reputation: 5009

Try the following:

Change <ColumnDefinition Width="2"/> to <ColumnDefinition Width="2*"/>

You're missing *.

Upvotes: 0

Related Questions