wpf 将 TextBlock 包裹在 Grid Column 中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13518179/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 06:20:16  来源:igfitidea点击:

Make a TextBlock wrap inside Grid Column

wpfxaml

提问by Drahcir

I have the following XAML which shows a textblock in a grid. The problem is that it just stretches out, it even stretches itself greater than the windows width.

我有以下 XAML,它在网格中显示一个文本块。问题是它只是伸展开来,它甚至伸展得比窗户的宽度还要大。

<Grid Background="Gray">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>

    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="Auto" />
    </Grid.ColumnDefinitions>

    <Label Grid.Row="1" Grid.Column="0" Padding="0" FontWeight="Bold" Margin="0,0,5,0">Description:</Label>
    <TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding Description}" TextWrapping="Wrap" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" />
</Grid>

回答by Rohit Vats

You need to restrict the widthof the second column to make the text wrap -

您需要restrict the width使用第二列来使文本换行 -

<Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>