提问人:ch m 提问时间:10/28/2023 最后编辑:ch m 更新时间:10/28/2023 访问量:39
文本框和文本块不会对齐
Textbox and Textblock won't line up
问:
我目前正在练习xaml。我打算用文本框和文本块制作一个简单的菜单。
当我将文本块或文本框拖到画布上时,它看起来还不错。但是在我运行程序后,它就很遥远了。我以为原因是保证金部分,但两个小时后,结果发现不是。
<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>
答:
尝试定义网格列和行,并将各个控件类对象放入其中,而不是松散地拖动它们 - 它们的对齐方式可能会受到其宽度和位置的阻碍。索引从 0 开始,如下所示:
<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>
行和列可以拟合到特定的宽度和/或高度,也可以简单地表示为根据包含的控制对象自动调整大小,或者填充与程序窗口和定义相比的其余空间。每个控件对象的行和列可以设置为行号或列号(同样,索引从 0 开始)。Auto
*
Grid.Row="x"
Grid.Column="x"
x
此外,您可以将对象包装在 WrapPanel 或 StackPanel 中,以将它们组合在一起而不是单独处理。
评论
使用s进行绝对定位是一些所见即所得的设计者在通过拖放移动控件时所做的事情,这确实是一个坏习惯,破坏了所有合理的布局(我认为Blend和VS-XAML设计器“Cider”也是如此。这就是为什么大多数 XAML“作者”更喜欢手动编写标记的原因。
您正在寻找的似乎是使用 和 在 WPF 网格中进行有序且干净的定位,以及 和 在控件的逻辑矩形内实现更崇高的对齐方式,并精确且精确地调整相邻元素之间的相对距离和所有 4 个 ltrb 方向。
以下 XAML 基本上可以执行您在第一个屏幕截图图像中似乎想要的布局。Margin
Grid.RowDefinitions
Grid.ColumnDefinitions
HorizontalAlignment
VerticalAlignment
Margin
<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>
<Grid.Resources>
<Style BasedOn="{StaticResource {x:Type TextBlock}}" TargetType="TextBlock">
<Setter Property="TextWrapping" Value="Wrap" />
<Setter Property="FontSize" Value="18" />
<Setter Property="MinHeight" Value="22" />
<Setter Property="Margin" Value="8,2,2,2" />
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="VerticalAlignment" Value="Center" />
</Style>
<Style BasedOn="{StaticResource {x:Type TextBox}}" TargetType="TextBox">
<Setter Property="TextWrapping" Value="Wrap" />
<Setter Property="Width" Value="100" />
<Setter Property="MinHeight" Value="22" />
<Setter Property="Margin" Value="8,2,2,2" />
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="VerticalAlignment" Value="Center" />
</Style>
<Style BasedOn="{StaticResource {x:Type ComboBox}}" TargetType="ComboBox">
<Setter Property="Width" Value="111" />
<Setter Property="MinHeight" Value="22" />
<Setter Property="Margin" Value="8,2,2,2" />
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="VerticalAlignment" Value="Center" />
</Style>
</Grid.Resources>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBox x:Name="textBox" Grid.Row="0" Grid.Column="0" Text="1" />
<ComboBox x:Name="comboBox" Grid.Row="0" Grid.Column="1">
<ComboBoxItem Content="Noodle" />
<ComboBoxItem Content="Rice" />
<ComboBoxItem Content="Burger" />
<ComboBoxItem Content="Pizza" />
</ComboBox>
<TextBlock x:Name="textCompany" Grid.Row="1" Grid.Column="0" Text="2" />
<TextBox x:Name="textBox1" Grid.Row="1" Grid.Column="1" Text="3" />
<TextBlock x:Name="textFrom" Grid.Row="2" Grid.Column="0" Text="4" />
<TextBox x:Name="txt" Grid.Row="2" Grid.Column="1" Text="5" />
<TextBlock x:Name="textBlock" Grid.Row="3" Grid.Column="0" Text="6" />
<TextBox x:Name="txtWho" Grid.Row="3" Grid.Column="1" Text="7" />
</Grid>
</Page>
请注意,我已将 TextBox、TextBlock 和 ComboBox 的所有元素通用的样式放入未键控的样式中。这样,您只需调整一个 setter,它适用于该类型的所有控件。
评论
Canvas
Grid