简体   繁体   English

XAML - 如何在 StackPanel 中将控件彼此对齐

[英]XAML - How do I align controls next to each other in a StackPanel

I've been trying to align the controls in the following code next to each other.我一直在尝试将以下代码中的控件彼此对齐。 But it doesn't seem to work.但它似乎不起作用。 First I had it without the StackPanel but then I couldn't really align my Controls properly.首先,我在没有 StackPanel 的情况下拥有它,但后来我无法真正正确对齐我的控件。

Here is my code:这是我的代码:

<StackPanel x:Name="doelenContentPanel" HorizontalAlignment="Center">
    <TextBlock x:Name="txtBOmzet" TextWrapping="Wrap" Text="Omzet:  " Margin="10,58,-10,-58" HorizontalAlignment="Left" />
    <TextBox x:Name="txtbOmzet" Margin="82,58,0,0" TextWrapping="Wrap" Text="" HorizontalAlignment="Center"/>
    <TextBlock x:Name="txtBOmzetMaand" Margin="151,58,0,0" TextWrapping="Wrap" Text="Maand" HorizontalAlignment="Right" />
</StackPanel>

First, don't use the hardcoded Margin property and second remove the HorizontalAlignment from each control.首先,不要使用硬编码的Margin属性,然后从每个控件中删除HorizontalAlignment Now to align the control next to each other, set the Orientation property of StackPanel现在要将控件彼此对齐,请设置StackPanel的 Orientation 属性

<StackPanel x:Name="doelenContentPanel" Orientation="Horizontal">
  <TextBlock x:Name="txtBOmzet" TextWrapping="Wrap" Text="Omzet:  "  />
  <TextBox x:Name="txtbOmzet" TextWrapping="Wrap" Text="" />
  <TextBlock x:Name="txtBOmzetMaand" TextWrapping="Wrap" Text="Maand" />
</StackPanel>

Got it!知道了! I added the Orientation="Horizontal" property to the StackPanel .我将Orientation="Horizontal"属性添加到StackPanel However, this only aligns the content of this specific panel.但是,这只会对齐此特定面板的内容。 If, for example, you want to have 2 StackPanel vertically aligned and then have the content of these 2 panels horizontally aligned, you have to do this:例如,如果您想让 2 个StackPanel垂直对齐,然后将这 2 个面板的内容水平对齐,则必须执行以下操作:

<StackPanel x:Name="contentWrapper" Orientation="Vertical">
    <StackPanel x:Name="doelenContentPanel" Orientation="Horizontal">
    <TextBlock x:Name="txtBOmzet" TextWrapping="Wrap" Text="Omzet:  "  />
    <TextBox x:Name="txtbOmzet" TextWrapping="Wrap" Text="" />
    <TextBlock x:Name="txtBOmzetMaand" TextWrapping="Wrap" Text="Maand" />
</StackPanel>
<StackPanel x:Name="secondPanel" Orientation="Horizontal">
    *this looks familiar to the first panel*
</StackPanel>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM