简体   繁体   English

在XAML中,如何将项目彼此相邻放置在网格中

[英]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 . 使用TextBlockGrid.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.

相关问题 如何在网格中放置一个 XAML 用户控件 - How to place an XAML usercontrol in a grid 如何覆盖不在(xaml / wpf)中的网格上的控件 - How do you overlay a control over a grid that it is not in (xaml/wpf) 如何在xaml中自动为数组的每个元素创建框? - How do you create box for each element of array automatically in xaml? 如何将具有依赖关系的项目组合到集合中的其他项目? - How do you group items that have dependencies to other items in a collection? ASP.NET Webform-如何将FormView和GridView彼此相邻放置 - ASP.NET Webform - How to place FormView and GridView next to each other 我如何使两个按钮彼此相邻 - How do I get two buttons next to each other 在控制台的列中将多个列表彼此相邻放置 - Place multiple lists next to each other in columns in console 是否可以像使用xaml中的网格一样,使用stackpanel进行星形调整? - Is it possible to apply star sizing with a stackpanel like you do with a grid in xaml? 给定两个字典,如何覆盖一个匹配项,另一个覆盖另一个项,而其余项保持不变 - Given two dictionaries, how do you overwrite matching items in one with items in the other, leaving the remaining items untouched 如何在 GRID XAML 中做一个正方形列表? - How to do a list of square in GRID XAML?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM