[英]In XAML how do you place items next to each other in a Grid
Ok Xaml is one of my weak points, so I would really appreciate some help on this...what I am trying to achieve is adding a title, name, surname in a listbox as follow: Ok Xaml是我的弱点之一,所以我真的很感激这方面的一些帮助...我想要实现的是在列表框中添加标题,名称,姓氏如下:
Mr. John Doe 约翰·多伊先生
Ms. John Doe 约翰·多伊女士
Mrs. Jane Doe 珍妮夫人
this is the xaml I have so far, and the result is the name title and surname are overlapping on top of each other: 这是我到目前为止的xaml,结果是名称标题和姓氏重叠在一起:
<DataTemplate>
<Grid>
<TextBlock Text="{Binding Title}" Width="60" Height="25" Margin="4" HorizontalAlignment="Left" />
<TextBlock Text="{Binding FirstName}" Width="60" HorizontalAlignment="Center" />
<TextBlock Text="{Binding LastName}" Width="60" HorizontalAlignment="Right" />
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
</Grid.ColumnDefinitions>
</Grid>
</DataTemplate>
In each TextBlock
you need to set Grid.Column="?"
在每个
TextBlock
您需要设置Grid.Column="?"
where ? 哪里? is 0, 1, or 2.
是0,1或2。
If you don't specify a column (or row), elements will by default go to (0,0). 如果未指定列(或行),则默认情况下元素将转到(0,0)。
Use the Grid.Column
attribute in the TextBlock
. 使用
TextBlock
的Grid.Column
属性。 Ie: 即:
<TextBlock Text="{Binding Title}" Width="60" Height="25" Margin="4" HorizontalAlignment="Left" Grid.Column="0"/>
<TextBlock Text="{Binding FirstName}" Width="60" HorizontalAlignment="Center" Grid.Column="1" />
<TextBlock Text="{Binding LastName}" Width="60" HorizontalAlignment="Right" Grid.Column="2" />
For each element in the grid, if you want them in an explicit Row or Column, you must specify them. 对于网格中的每个元素,如果要在显式行或列中使用它们,则必须指定它们。 To do this you would use
Grid.Row="X"
or Grid.Column="X"
. 为此,您将使用
Grid.Row="X"
或Grid.Column="X"
。 If you leave these off, the default value is 0. 如果将其保留为关闭状态,则默认值为0。
In your case, you would want to do the following. 在您的情况下,您可能希望执行以下操作。
<DataTemplate>
<Grid>
<TextBlock Text="{Binding Title}" Width="60" Height="25" Margin="4" HorizontalAlignment="Left" Grid.Column="0" />
<TextBlock Text="{Binding FirstName}" Width="60" HorizontalAlignment="Center" Grid.Column="1" />
<TextBlock Text="{Binding LastName}" Width="60" HorizontalAlignment="Right" Grid.Column="2" />
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
</Grid.ColumnDefinitions>
</Grid>
</DataTemplate>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.