0

您好需要在 Windows Phone 的 xaml 列表框中将 6 个文本块居中。我写了这段代码

    <!--ContentPanel - inserire ulteriore contenuto qui-->
    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <ListBox x:Name="CountryListBox" HorizontalAlignment="Left"  Margin="6,6,0,151" Width="444">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Height="Auto" Width="500" HorizontalAlignment="Center">

                        <TextBlock Text="{Binding Word}" SizeChanged="TextBox_SizeChanged" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}" HorizontalAlignment="Center" FontWeight="Bold" Foreground="Red" FontSize="64"/>
                        <TextBlock Text="{Binding Parola1}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}" Height="70"  HorizontalAlignment="Center"/>
                        <TextBlock Text="{Binding Parola3}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}" Height="70"  HorizontalAlignment="Center"/>
                        <TextBlock Text="{Binding Parola4}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}" Height="70"  HorizontalAlignment="Center"/>
                        <TextBlock Text="{Binding Parola5}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}" Height="70"  HorizontalAlignment="Center"/>
                        <TextBlock Text="{Binding Parola2}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}" Height="70"  HorizontalAlignment="Center"/>

                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
        <Button Content="Giusto" Height="72" HorizontalAlignment="Left" Margin="47,462,0,0" Name="button1" VerticalAlignment="Top" Width="160" Click="button1_Click" />
        <Button Content="Sbagliato" Height="72" HorizontalAlignment="Left" Margin="251,462,0,0" Name="button2" VerticalAlignment="Top" Width="160" />
    </Grid>

但我想用自动宽度和高度来做到这一点。有什么建议么?我还有一个小问题..如何在不使用其他对象的情况下自动调整文本块的字体大小?谢谢

4

1 回答 1

0

如果您想要自动高度和宽度,只需设置Height="Auto"Width="Auto"。我没有得到你所期望的。最好显示正在发生的事情以及您所期望的屏幕截图。

此外,如果您希望文本块居中对齐,请尝试设置属性HorizontalContentAlignment="Center"

根据评论编辑:当您设置 时TextWrapping="Wrap",文本将继续到下一行。不幸的是,Windows phone 中没有字体自动调整大小选项。也许你可以尝试使用转换器,像这样

<TextBlock Text="{Binding Word}" FontSize={{Binding Path=Word, Converter={StaticResource TexttoFontSizeConverter}}" />

在转换器中你可以做这样的事情

public class TexttoFontSizeConverter : IValueConverter
{
    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter,
        System.Globalization.CultureInfo culture)
    {
        var text = (string)value;
        if (text.Length > 30)
            return 20;
        else
            return 30; //20 ad 30 are the font sizes
    }

    public object ConvertBack(object value, Type targetType, object parameter,
        System.Globalization.CultureInfo culture)
    {
        throw new NotSupportedException();
    }

    #endregion
}

注意:我没有测试过这个

于 2012-11-27T07:35:16.553 回答