UWP XAML - 创建同样宽的TextBlocks

时间:2016-06-20 22:33:02

标签: xaml windows-phone uwp

我有一个学生对象列表,显示如下:

ListView

我希望名字和数字之间有空格,但我怎么能意识到呢?

XAML代码:

  

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" >

    <ListView x:Name="StudentsList" ItemsSource="{Binding Studenten}">


        <ListView.ItemTemplate>

            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding Nachname}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
                    <TextBlock Text="{Binding Vorname}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
                    <TextBlock Text="{Binding MatrikelNr}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
                </StackPanel>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</Grid> </Page>

2 个答案:

答案 0 :(得分:0)

您可以使用保证金。

Margin = "Left,Up,Right,Down"

或者您可以为所有方向使用一个值

Margin = "Value"

所以,你的XAML应该是这样的:     

    <ListView x:Name="StudentsList" ItemsSource="{Binding Studenten}">


        <ListView.ItemTemplate>

            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding Nachname}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="5"/>
                    <TextBlock Text="{Binding Vorname}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="5"/>
                    <TextBlock Text="{Binding MatrikelNr}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="5"/>
                </StackPanel>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</Grid> </Page>

答案 1 :(得分:0)

如果你在每个名字后面有一个固定的空格(比如...... 10pixels),那么你可以使用Kinani给出的答案,只需在你的第3个TextBlock中添加一个边距(比如说Margin="10,0,0,0")数字绑定在其中。

如果您希望将数字对齐到右侧以使列表更易于阅读,那么您可以执行以下操作:

<Grid Orientation="Horizontal">
     <Grid.ColumnDefinitions>
          <ColumnDefinition Width="Auto">
          <ColumnDefinition Width="Auto">
          <ColumnDefinition>
     </Grid.ColumnDefinitions>
     <TextBlock Text="{Binding Nachname}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="0,0,5,0"/>
     <TextBlock Grid.Column="1" Text="{Binding Vorname}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="0,0,5,0"/>
     <TextBlock Grid.Column="2" Text="{Binding MatrikelNr}" HorizontalAlignment="Right" VerticalAlignment="Stretch" />
</Grid>

这会产生类似于此的外观:

FirstName SecondName 123456789

FName SName 012345678