Search code examples
c#xamltextboxtextblock

Textbox and Textblock won't line up


I'm currently practicing xaml. I was going to make a simple menus with textbox and textblock.

When I drag textblock or textbox to the canvas, it is look ok. But it is way off after I run the program. I thought the reason is the Margin portion but after two hours, it turned out it is not.

What am I missing here? editing view after run view

<Page
    x:Class="newPrac.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:newPrac"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Grid>
        <ComboBox x:Name="comboBox" Margin="218,56,0,0" Width="111" Height="42">
            <ComboBoxItem Content="Noodle"/>
            <ComboBoxItem Content="Rice"/>
            <ComboBoxItem Content="Burger"/>
            <ComboBoxItem Content="Pizza"/>
        </ComboBox>
        <TextBlock x:Name="textCompany" Margin="57,110,1114,571" TextWrapping="Wrap" Text="2" FontSize="18"/>
        <TextBlock x:Name="textFrom" Margin="57,190,1127,486" TextWrapping="Wrap" Text="4" FontSize="18" />
        <TextBlock x:Name="textBlock" Margin="58,290,1136,388" TextWrapping="Wrap" Text="6" FontSize="18"/>
        
        <TextBox x:Name="textBox" Margin="41,55,1139,622" TextWrapping="Wrap" Text="1" Width="100"/>
        <TextBox x:Name="textBox1" Margin="229,129,951,547" TextWrapping="Wrap" Text="3" Width="100"/>
        <TextBox x:Name="txt" Margin="229,190,951,485" TextWrapping="Wrap" Text="5" Width="100"/>
        
        <TextBox x:Name="txtWho" Margin="217,290,963,385" TextWrapping="Wrap" Text="TextBox" Width="100"/>
    </Grid>
</Page>

Solution

  • Try defining your grid columns and rows and fit the individual control class objects in there instead of loosely dragging them - their alignment might be obstructed by their width and positons. Indexing starts at 0 and looks something like this:

    <Grid>
        <Grid.ColumnDefinitions>
            <!--COLUMN DEFINITIONS (SET SIZE FOR EACH WITH = VALUE, AUTO OR *-->
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <!--ROW DEFINITIONS (SET SIZE FOR EACH WITH = VALUE, AUTO OR *-->
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>
    </Grid>
    

    The rows and columns can be fitted with to specific widths and/or heights or can simply be denoted with Auto for automatic sizing depending on the containing control objects or * to fill in the rest of the space compared to the program window and definitons. The row and column for each control object can be set with Grid.Row="x" or Grid.Column="x" where x would be the row or column number (again, indexing starts at 0).

    Furthermore, you could wrap the objects in a WrapPanel or StackPanel to treat them collectively rather than individually.