[英]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 第二:说明
TextBlock
displaying "Linux" has to be linked to the Ubuntu TextBlock
. 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}"
TextBlock
with a 36 FontSize
fitting this given width. FontSize
的TextBlock
。 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.