22

我有一个 StackPanel,但下面一行:

<TextBlock Grid.Row="1" Grid.Column="0" Text="{Binding Notes}" TextWrapping="Wrap"  />

不是包装文本。

<StackPanel Orientation="Vertical">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="auto" />
            <ColumnDefinition Width="auto" />
            <ColumnDefinition Width="auto" />
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="auto" />
            <ColumnDefinition Width="5" />
            <ColumnDefinition Width="15" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>

        <DockPanel Grid.Row="0" Grid.Column="0">
            <TextBlock FontWeight="Bold" Padding="0,0,5,0" Text="{Binding Path=Id, StringFormat='#\{0\}'}" />
            <TextBlock FontWeight="Bold" Padding="0,0,5,0" Text="{Binding Path=Name}" />
        </DockPanel>
        <TextBlock Grid.Row="0" Grid.Column="4" FontWeight="Bold" Text="{Binding Path=Time, StringFormat={}{0:HH:mm}}" />
        <Image
            Grid.Row="0"
            Grid.Column="6"
            HorizontalAlignment="Center"
            VerticalAlignment="Center"
            Source="{Binding Path=Image, Mode=OneWay, Converter={StaticResource ImageConverter}}" />

        <TextBlock Grid.Row="1" Grid.Column="0" Text="{Binding Notes}" TextWrapping="Wrap" />

        <Image
            Grid.Row="1"
            Grid.Column="4"
            HorizontalAlignment="Center"
            VerticalAlignment="Top"
            Source="{Binding Path=Picture, Mode=OneWay, Converter={StaticResource PictureConverter}}" />
    </Grid>
</StackPanel>

StackPanel方向设置为“垂直”,但没有TextBlock继承它。

我哪里错了?

4

1 回答 1

22

您的问题是使用StackPanel允许其子级填充所有可用空间的 —— 相应StackPanel地拉伸其内容的大小。尝试删除StackPanel并保留Grid- 这样您将限制其子项的大小,以限制网格使用的可用空间。

如果在您构建的布局中这还不够,请尝试在TextBox需要换行的地方设置 MaxWidth。

现在您的问题的根源还在于您被插入到具有无限大小(Width =“Auto”)TextBox的第一列中。Grid因此,将 Grid.Column="7" 设置为TextBox您想要的技巧(包装文本)。这是修改后的代码:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="5" />
        <ColumnDefinition Width="15" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition />
    </Grid.RowDefinitions>

    <DockPanel Grid.Row="0" Grid.Column="0">
        <TextBlock FontWeight="Bold" Padding="0,0,5,0" Text="{Binding Path=Id, StringFormat='#\{0\}'}" />
        <TextBlock FontWeight="Bold" Padding="0,0,5,0" Text="{Binding Path=Name}" />
    </DockPanel>
    <TextBlock Grid.Row="0" Grid.Column="4" FontWeight="Bold" Text="{Binding Path=Time, StringFormat={}{0:HH:mm}}" />
    <Image
        Grid.Row="0"
        Grid.Column="6"
        HorizontalAlignment="Center"
        VerticalAlignment="Center"
        Source="{Binding Path=Image, Mode=OneWay, Converter={StaticResource ImageConverter}}" />

    <TextBlock Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="7" Text="{Binding Notes}" TextWrapping="Wrap" />

    <Image
        Grid.Row="1"
        Grid.Column="4"
        HorizontalAlignment="Center"
        VerticalAlignment="Top"
        Source="{Binding Path=Picture, Mode=OneWay, Converter={StaticResource PictureConverter}}" />
</Grid>
于 2012-07-11T11:53:19.780 回答