简体   繁体   English

限制旋转的TextBlock大小

[英]Limit rotated TextBlock size

I am trying to limit the first column height to match the second one's after rotating the TextBlock. 在旋转TextBlock之后,我试图将第一列的高度限制为与第二列的高度匹配。

I have the following XAML: 我有以下XAML:

<Viewbox>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>

        <TextBlock
            Grid.Column='1'
            Text='Ubuntu'
            FontFamily='Ubuntu'
            FontSize='36' />

        <TextBlock
            Grid.Column='0'
            Text='Linux'
            FontFamily='Ubuntu'
            FontSize='36'>
            <TextBlock.LayoutTransform>
                <RotateTransform
                    Angle='-90' />
            </TextBlock.LayoutTransform>
        </TextBlock>

    </Grid>
</Viewbox>

Which will render the following: 这将呈现以下内容:

实际产量

However, I'm trying to get this output, so that the height of the left column will adapt to that of the second: 但是,我正在尝试获取此输出,以使左列的高度适应于第二列的高度:

所需的输出

Have you got any idea how to do that purely with declarative XAML? 您是否知道如何仅使用声明性XAML做到这一点? Ie. 就是 no binding to heights or code-behind. 没有绑定到高处或代码隐藏。 I also do not want to specify any margins of the controls. 我也不想指定控件的任何边距。

Thanks. 谢谢。

Slight modifications in your code can give you what you want: 在代码中进行少量修改可以为您提供所需的内容:

First: your updated code 第一:您更新的代码

<Grid >
            <Grid.ColumnDefinitions>
                <ColumnDefinition />
                <ColumnDefinition />
            </Grid.ColumnDefinitions>

            <TextBlock x:Name="UbuntuBox"
            Grid.Column='1'
            Text='Ubuntu'
            FontFamily='Ubuntu'
            FontSize='36' />

            <TextBlock Width="{Binding ElementName=UbuntuBox, Path=Height}"
            Grid.Column='0'
            Text='Linux'
            FontFamily='Ubuntu'
            >
            <TextBlock.LayoutTransform>
                <RotateTransform
                    Angle='-90' />
            </TextBlock.LayoutTransform>
            </TextBlock>

        </Grid>

Second: Explanations 第二:说明

  • Your TextBlock displaying "Linux" has to be linked to the Ubuntu TextBlock . 您的显示“ Linux”的TextBlock必须链接到Ubuntu TextBlock In this case, its Width must be the same as Ubuntu's Height , so I added the following: Width="{Binding ElementName=UbuntuBox, Path=Height}" 在这种情况下,其Width必须与Ubuntu的Height相同,因此我添加了以下内容: Width="{Binding ElementName=UbuntuBox, Path=Height}"
  • You can't have a TextBlock with a 36 FontSize fitting this given width. 您不能拥有适合此给定宽度的36 FontSizeTextBlock Removing it will keep it to default, then you can play with it if you want 删除它会使它保持默认状态,然后您可以根据需要使用它

And that's all you need! 这就是您所需要的! No hardcoded added stuff there =) 那里没有硬编码添加的东西=)

This is the best i could come up with. 这是我能想到的最好的方法。 The bad part is the hardcoded row definition, but because you already have the root grid inside a viewbox, its relative, so it shouldn't be a big problem. 不好的部分是硬编码的行定义,但是因为您已经在视图框内有了根网格及其相对的根网格,所以它应该不是一个大问题。

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="36"/>
    </Grid.RowDefinitions>
    <TextBlock
        Grid.Column='1'
        Text='Ubuntu'
        FontFamily='Ubuntu'
        FontSize='36' />

    <Viewbox Grid.Column='0'>
        <TextBlock
            Text='Linux'
            FontFamily='Ubuntu'
            FontSize='36'>
            <TextBlock.LayoutTransform>
                <RotateTransform
                    Angle='-90' />
            </TextBlock.LayoutTransform>
        </TextBlock>
    </Viewbox>
</Grid>

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

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