文本换行会扩展列以适合文本

发布于 2024-07-20 10:45:51 字数 660 浏览 6 评论 0 原文

我有一个简单定义的网格:

<Grid Margin="0,5,0,0">
  <Grid.ColumnDefinitions>
    <ColumnDefinition Width="50"></ColumnDefinition>
    <ColumnDefinition Width="50"></ColumnDefinition>
    <ColumnDefinition Width="48"></ColumnDefinition>
    <ColumnDefinition Width="Auto"></ColumnDefinition>
   </Grid.ColumnDefinitions>

然后我尝试绑定一些像这样的内容:

<TextBlock TextWrapping="Wrap" Grid.Column="3" Text="{Binding Text}">

像这样设置,文本不会换行。 它只是扩展列以适合文本。 如果我将最后一列的宽度设置为固定值,则换行将按预期进行。 问题在于,如果用户加宽窗口,该列将保持固定大小。 如何使列的大小根据网格的宽度动态调整,但仍将文本包裹在其中?

I've got a grid defined simply:

<Grid Margin="0,5,0,0">
  <Grid.ColumnDefinitions>
    <ColumnDefinition Width="50"></ColumnDefinition>
    <ColumnDefinition Width="50"></ColumnDefinition>
    <ColumnDefinition Width="48"></ColumnDefinition>
    <ColumnDefinition Width="Auto"></ColumnDefinition>
   </Grid.ColumnDefinitions>

Then I'm trying to bind some content like this:

<TextBlock TextWrapping="Wrap" Grid.Column="3" Text="{Binding Text}">

Set up like this, the text won't wrap. It simply expands the column to fit the text. If I set the Width to a fixed amount on the last column, wrapping works as expected. The problem there is that if the user widens the window, the column stays at a fixed size. How can I get the column to size dynamically with the width of the grid, but still wrap the text within it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

酷到爆炸 2024-07-27 10:45:51

"*" 的宽度将使用 "*" 在列之间均匀分割剩余空间。 如果您有一个带有 Width="*" 的列,则该列将获得所有剩余空间。 如果您有 2 列 Width="*",则每列将获得剩余空间的 1/2。

这是一篇关于网格大小调整的好文章,其中包括星形大小调整。

A width of "*" will split any remaining space evenly between the columns using "*". If you have a single column with Width="*", that column will get all remaining space. If you have 2 columns with Width="*", each will get 1/2 of the remaining space.

Here's a good article on grid sizing that includes star sizing.

在梵高的星空下 2024-07-27 10:45:51

我发现有一个令人沮丧的情况,当您拥有 收支平衡.windows.controls.grid.issharedsizescope%28v=vs.85%29.aspx" rel="noreferrer">IsSharedSizeScope= true

<Border BorderBrush="Red" BorderThickness="1">
    <StackPanel Grid.IsSharedSizeScope="True">

        <Grid HorizontalAlignment="Stretch">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" SharedSizeGroup="G1"/>
                <ColumnDefinition Width="Auto" SharedSizeGroup="G2" />
                <ColumnDefinition Width="*"  SharedSizeGroup="G3" />
            </Grid.ColumnDefinitions>

            <TextBlock Text="Col0" Grid.Column="0" Margin="0,0,5,0"/>
            <TextBlock Text="Col1" Grid.Column="1" Margin="0,0,5,0"/>

            <TextBlock Text="A B C D E F G H I J K L M N O P Q R S T U V W X Y Z" TextWrapping="Wrap" Grid.Column="2"/> 
        </Grid>

    </StackPanel>                   

</Border>

这不会换行,但如果您将 Grid.IsSharedScopeSize 更改为 false 那么它就会换行。

尚未找到解决方案,但这可能是它不起作用的另一个原因。

There's one frustrating case I discovered that can break even with Width="*" and thats when you have IsSharedSizeScope= true.

<Border BorderBrush="Red" BorderThickness="1">
    <StackPanel Grid.IsSharedSizeScope="True">

        <Grid HorizontalAlignment="Stretch">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" SharedSizeGroup="G1"/>
                <ColumnDefinition Width="Auto" SharedSizeGroup="G2" />
                <ColumnDefinition Width="*"  SharedSizeGroup="G3" />
            </Grid.ColumnDefinitions>

            <TextBlock Text="Col0" Grid.Column="0" Margin="0,0,5,0"/>
            <TextBlock Text="Col1" Grid.Column="1" Margin="0,0,5,0"/>

            <TextBlock Text="A B C D E F G H I J K L M N O P Q R S T U V W X Y Z" TextWrapping="Wrap" Grid.Column="2"/> 
        </Grid>

    </StackPanel>                   

</Border>

This won't wrap, but if you change Grid.IsSharedScopeSize to false then it does.

Haven't found a solution yet, but this can be another reason it won't work.

北音执念 2024-07-27 10:45:51

尝试这个:

<Grid Margin="0,5,0,0">
  <Grid.ColumnDefinitions>
    <ColumnDefinition Width="50"></ColumnDefinition>
    <ColumnDefinition Width="50"></ColumnDefinition>
    <ColumnDefinition Width="48"></ColumnDefinition>
    <ColumnDefinition Name="ParentColumn" Width="Auto"></ColumnDefinition>
   </Grid.ColumnDefinitions>
   <TextBlock TextWrapping="Wrap" Grid.Column="3" Text="{Binding Text}"
      MaxWidth="{Binding ActualWidth, ElementName=ParentColumn}">

Try this:

<Grid Margin="0,5,0,0">
  <Grid.ColumnDefinitions>
    <ColumnDefinition Width="50"></ColumnDefinition>
    <ColumnDefinition Width="50"></ColumnDefinition>
    <ColumnDefinition Width="48"></ColumnDefinition>
    <ColumnDefinition Name="ParentColumn" Width="Auto"></ColumnDefinition>
   </Grid.ColumnDefinitions>
   <TextBlock TextWrapping="Wrap" Grid.Column="3" Text="{Binding Text}"
      MaxWidth="{Binding ActualWidth, ElementName=ParentColumn}">
柒七 2024-07-27 10:45:51

将其宽度设置为“*”

Set its width to "*"

心舞飞扬 2024-07-27 10:45:51

仅当您希望根据所述列/行中的内容调整列/行的大小时,才应使用“自动”。 如果您想“分配剩余空间”,请使用“*”。
在您的情况下,TextBlock 需要知道在实际测量之前它有多少空间,以便它可以告诉在哪里换行文本。

You should use Auto only when you want to column/row to size depending on content in said column/row. If you want to "asign rest of space" use "*".
In your case, TextBlock needs to know how much space it has before acutal measure, so it can tell where to wrap the text.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文