简体   繁体   English

网格上的左水平对齐

[英]Left HorizontalAlignment on a Grid

I'm trying to use HorizontalAlignment="Left" in the following situation: 我正在尝试在以下情况下使用Horizo​​ntalAlignment =“ Left”:

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
  <Grid HorizontalAlignment="Left">
    <Grid.ColumnDefinitions>
      <ColumnDefinition />
      <ColumnDefinition />
      <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <TextBlock Grid.Column="0" Background="Gray" Text="Small Text"  TextWrapping="Wrap"/>
    <TextBlock Grid.Column="1" Background="White" Text="This is a very large amount of text"  TextWrapping="Wrap"/>
    <TextBlock Grid.Column="2" Background="Gray" Text="Medium amount of text" TextWrapping="Wrap"/>
  </Grid>
</Window>

My goal is to be able to resize the window, and have the three TextBlocks resize themselves proportionally. 我的目标是能够调整窗口大小,并使三个TextBlocks按比例调整大小。 This works, but the grid is putting some blank space to the right of the final column, and as I try to resize towards the final column, the columns start to shrink. 这项工作可行,但是网格在最后一列的右边放置了一些空白,并且当我尝试朝最后一列调整大小时,列开始缩小。 I want this shrinking behavior, but I don't want it to start until there is no more white space to the right of the rightmost column. 我想要这种缩小的行为,但是我不希望它开始,直到最右边的列的右边不再有空白为止。

I can't use a UniformGrid as the text lengths can vary, and no other built-in WPF control that I've seen has the ability to resize all children when the parent size changes. 我不能使用UniformGrid,因为文本长度可能会有所不同,而且我看到的其他内置WPF控件也无法在父级大小更改时调整所有子级的大小。 I've looked into creating a custom panel, but that seems to be more trouble than it's worth. 我已经考虑创建一个自定义面板,但这似乎比其价值更大。 I feel like something much more simple can be done here. 我觉得这里可以做一些简单得多的事情。

Any suggestions or ideas are appreciated. 任何建议或想法表示赞赏。

You'll have to build your own custom panel, and handle the case where the AvailableWidth is less then the panel's children DesiredWidth 您必须构建自己的自定义面板,并处理AvailableWidth小于面板子DesiredWidth

Panel layout in WPF (Grid is a Panel) is a 2 step process, in the first pass the Panel iterates over its children and provides them with the panel's AvailableWidth . WPF(网格是面板)中的面板布局是一个两步过程,在第一步中,面板迭代其子代并为其提供面板的AvailableWidth The children respond to this by computing their DesiredWidth . 孩子们通过计算DesiredWidth对此做出响应。

In the second pass the Panel arranges the children according to their DesiredWidth . 在第二遍中,小组根据孩子的DesiredWidth安排孩子。 In this second pass you have access to the width that all the children (in your case, TextBlocks) require. 在第二遍中,您可以访问所有子级(在您的情况下为TextBlocks)所需的宽度。 If it is less than the panel's available width you can compute a percentage to give each one so that they appear to shrink uniformly. 如果它小于面板的可用宽度,则可以计算一个百分比以给出每个百分比,以使它们看起来均匀收缩。

Here's a resource that shows how you can create your own custom panel 这是显示如何创建自己的自定义面板的资源

What about this? 那这个呢?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM