wpf 如何在宽度自动列内 TextWrap 文本块?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16900416/
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-08 23:49:07  来源:igfitidea点击:

How to TextWrap a TextBlock within a width Auto Column?

wpfxamlsilverlighttextblock

提问by King Chan

Consider something as follows:

考虑如下:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="Auto"/>
    </Grid.ColumnDefinitions>
    <TextBlock x:Name="WrapTextBlock" Grid.Column="0" Text="123 456 789 0123 4456 123  123  123  123 1 23  123 " TextWrapping="Wrap" />
    <TextBlock x:Name="NotWrapTextBlock" Grid.Column="1" Text="GGG" />
</Grid>

This XAML will allow WrapTextBlocktext to be wrap, doing this, WrapTextBlockwill take all the space and push NotWrapTextBlockto the right.

此 XAML 将允许WrapTextBlock文本换行,这样做WrapTextBlock将占用所有空间并NotWrapTextBlock向右推。

But what I want to do is to have WrapTextBlocktake as less space as possible, pushing NotWrapTextBlockright after WrapTextBlockand fill the right side with empty space.

但我想要做的是WrapTextBlock尽可能少地占用空间,紧随NotWrapTextBlock其后WrapTextBlock并用空白空间填充右侧。

Which means the following:

这意味着以下内容:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <TextBlock x:Name="WrapTextBlock" Grid.Column="0" Text="123 456 789 0123 4456 123  123  123  123 1 23  123 " TextWrapping="Wrap" />
    <TextBlock x:Name="NotWrapTextBlock" Grid.Column="1" Text="GGG" />
</Grid>

But the thing here is, now the text in WrapTextBlockwouldn't wrap anymore.

但这里的事情是,现在文本WrapTextBlock不会再换行了。

I mean something like follows:

我的意思是这样的:

When text is too long it requires to warp:

当文本太长时,它需要扭曲:

When text is short enough that doesn't requires to warp:

当文本足够短不需要变形时:

回答by Chris W.

The reason is by defining your ColumnDefinitionas Auto or * you have nothing to limit the size available for your TextBlockto consume. So it would be expected behavior for the Text to not Wrap. So you'll have to define a Widthor MaxWidthon either the ColumnDefinitionor the TextBlockdirectly. So for instance;

原因是通过将您定义ColumnDefinition为 Auto 或 * 您没有任何限制可供您TextBlock使用的大小。因此,文本不换行是预期的行为。因此,您必须直接在 the或 the上定义 aWidth或。例如;MaxWidthColumnDefinitionTextBlock

<Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" MaxWidth="50"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <TextBlock x:Name="WrapTextBlock" Text="123 456 789 0123 4456 123  123  123  123 1 23  123 " TextWrapping="Wrap" />
        <TextBlock x:Name="NotWrapTextBlock" Grid.Column="1" Text="GGG" />
    </Grid>

Will give you your wrap, if you want to say instead only allow it to take for example say, 7% of the space the grid has to use, change the Widthto something like;

会给你你的包裹,如果你想说只允许它以例如说,网格必须使用的空间的7%,改变Width为类似的东西;

<Grid.ColumnDefinitions>
  <ColumnDefinition Width="0.07*"/>
  <ColumnDefinition Width="0.93*"/>
</Grid.ColumnDefinitions>

So the first column will take up 7% of the space available, and the right column will consume the rest. Hope this helps.

因此,第一列将占用可用空间的 7%,而右列将占用其余空间。希望这可以帮助。

Edit Addition:

编辑补充:

What you're showing pretty much aligns with your first snippet wherein the first column should push, the second one should only allow enough space for its content to show;

您所展示的内容与您的第一个片段几乎一致,其中第一列应该推送,第二列应该只允许足够的空间来显示其内容;

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="Auto"/>
    </Grid.ColumnDefinitions>
    <TextBlock x:Name="WrapTextBlock" Grid.Column="0" Text="123 456 789 0123 4456 123  123  123  123 1 23  123 " TextWrapping="Wrap" />
    <TextBlock x:Name="NotWrapTextBlock" Grid.Column="1" Text="GGG" />
</Grid>