c# - 如何使我的 TextBlocks 在我的 C# XAML Windows 8.1 应用程序中具有正确的大小?

标签 c# xaml windows-phone-8.1 winrt-xaml windows-8.1

我是 Windows 8.1 开发的新手,如果这个问题很明显或很初级,请原谅我。我已经对各种属性进行了相当多的试验,但我无法让它按照我想要的方式工作,所以我想也许这里有人可以帮我一把。

我在做什么的描述: 我制作了一个 UserControl,这样我就可以为我正在尝试做的这件事隔离 UI,而且我还可以在我的真实 UI 的各个地方使用它。 UserControl 由一个 Button 和一个 Grid 组成。 Grid 有 3 行的定义,我希望这些行(以及它们包含的 Border 元素和 TextBlock 元素)采用第 0 行、第 1 行和第 2 行分别增加总高度的 20%、60% 和 20%。我还希望 Grid 占据 Button 的整个高度。

这是 UserControl 的 XAML 标记,颜色鲜艳,所以很明显一切都在哪里。

<UserControl
    x:Name="UserControlRoot"
    d:DesignHeight="300"
    d:DesignWidth="400"
    mc:Ignorable="d"
>
    <Grid x:Name="LayoutRoot" Margin="0" Background="Red">
        <Grid.Resources>
            <Style TargetType="TextBlock" x:Key="UCTextBlock">
                <Setter Property="FontSize" Value="20" />
                <Setter Property="TextAlignment" Value="Center" />
                <Setter Property="TextWrapping" Value="NoWrap" />
            </Style>
        </Grid.Resources>
        <!-- THIS BUTTON HAS SPACING AROUND IT - WHY? -->
        <Button 
            VerticalAlignment="Stretch"
            HorizontalAlignment="Stretch"
            BorderBrush="White"
            BorderThickness="5"
            Background="Green"
            Padding="0"
            Foreground="Blue"
            Margin="0"
        >
            <Grid Background="#ff333333">
                <Grid.RowDefinitions>
                    <RowDefinition Height="2*" />
                    <RowDefinition Height="6*" />
                    <RowDefinition Height="2*" />
                </Grid.RowDefinitions>

                <Border Grid.Row="0" HorizontalAlignment="Stretch">
                    <TextBlock Text="THIS IS MY RED TEXT" Foreground="LightCoral" Style="{StaticResource UCTextBlock}" />
                </Border>
                <Border Grid.Row="1" HorizontalAlignment="Stretch">
                    <TextBlock Text="THIS IS MY WHITE TEXT, NOT TALL" Foreground="White" Style="{StaticResource UCTextBlock}" />
                </Border>
                <Border Grid.Row="2" HorizontalAlignment="Stretch">
                    <TextBlock Text="THIS IS MY BLUE TEXT" Foreground="LightSkyBlue" Style="{StaticResource UCTextBlock}" />
                </Border>
            </Grid>
        </Button>
    </Grid>
</UserControl>

这是它在设计器中的样子:

Screenshot of the designer, with three TextBlocks, which are all the same height

请注意,第 1 行 (Height="6*") 中的白色文本不是第 0 行和第 2 行的 3 倍高。据我了解, Height="6*"vs Height="2*"应该使第 1 行大于第 0 行和第 2 行。我显然在这里错过了一些明显的东西,但我不知道它是什么。

关于为什么 TextBlocks 没有按照我想要的方式调整大小有什么想法吗?没有影响任何元素样式的外部设计模板(据我所知,尽管我不确定是否有办法明确/详尽地确定情况是否确实如此)。

一个几乎不相关的旁白:如果这对其他人有帮助,我发现在 Button 上放置 Margin="0,-10"将摆脱按钮周围的红色区域外部网格。花了一段时间才弄明白,所以希望它能为其他人节省一些时间。可能有一个 ThemeResource 具有相同的效果,但如果是的话,我从未找到它。

最佳答案

在 Button 中,将水平和垂直内容对齐设置为 Stretch。这将拉伸(stretch)内容网格。

<Button HorizontalContentAlignment="Stretch" ...
</Button>

关于c# - 如何使我的 TextBlocks 在我的 C# XAML Windows 8.1 应用程序中具有正确的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29834368/

相关文章:

c# - 删除行后 XtraGrid 行索引不匹配

c# - WPF DataTemplate 绑定(bind)取决于属性的类型

c# - 依赖属性 - 无法从 XAML 设置值

xaml - 网格不占用 100% 宽度

c# - 在 Windows Phone 8.1 中使用什么进行 Paypal 付款

c# - 将 2 个连续字节转换为一个 int 值提高 C# 中的速度

c# - 为什么 foreach on PlaceHolder.Controls 抛出 "Collection was modified"?

c# - 如果我的应用程序中需要有 60 个图像控件,我是否必须有 60 个这样巨大的代码块?

android - 移动设备(Android、iOS、Windows Phone)上的 TensorFlow

c# - 为什么 HashAlgorithm 类要实现 IDisposable?