WPF - 在网格单元格中追加字符串内容

时间:2015-01-19 18:57:10

标签: wpf grid alignment

我有一个包含2X2网格的工具提示(请参阅下面的代码)。第1行col 1中的值是动态分配的,但我想将它附加到左侧单元格中的字符串。

示例:用户应该看到:

默认值:0.5,最小值:0,最大值:1092,当前值:2

但现在它写着:

默认值:0.5,最小值:0,最大值:1092,当前:(此处有多个空格)2

似乎应该很简单,但我无法让它发挥作用。或者,我想至少将此值与单元格的左侧对齐,因为它当前看起来位于中心,但Horizo​​ntalContentAlignment =“Left”似乎不起作用。有什么想法吗?

<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Label Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="2">Time after removing all keycards that the relay will switch states</Label>
<Label Grid.Column="0" Grid.Row="1" >Default: 0.5, Min: 0, Max: 1092, Current:</Label>
<Label Grid.Column="1" Grid.Row ="1">
<Label.Content>
<Binding Path="EgressTimeout"
Converter="{StaticResource timeConverter}" />
</Label.Content>
</Label>
</Grid>

1 个答案:

答案 0 :(得分:0)

您所看到的问题是因为没有定义网格和/或列宽,它会使它们具有相同的大小并占用容器中的所有可用空间。 &#34;多个空格&#34;你看到的是第一列的剩余宽度,它是整个容器宽度的50%。添加ShowGridLines =&#34; true&#34;看看我的意思。

要消除额外空间,可以将第一列宽度设置为“自动”或其他一些定义的宽度。

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition />
    </Grid.RowDefinitions>
    <Grid x:Name="yourOriginalGrid" ShowGridLines="True">
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>
        <Label Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="2">Time after removing all keycards that the relay will switch states</Label>
        <Label Grid.Column="0" Grid.Row="1" >Default: 0.5, Min: 0, Max: 1092, Current:</Label>
        <Label Grid.Column="1" Grid.Row ="1">1492</Label>
    </Grid>
    <Grid Grid.Row="1" Background="Azure" ShowGridLines="True">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <TextBlock Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="2">Time after removing all keycards that the relay will switch states</TextBlock>
        <TextBlock Grid.Column="0" Grid.Row="1" >Default: 0.5, Min: 0, Max: 1092, Current:</TextBlock>
        <TextBlock Grid.Column="1" Grid.Row ="1" Margin="2 0">1492</TextBlock>
    </Grid>
</Grid>