WPF 旋转文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20994663/
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
WPF Rotate text
提问by user1253414
I′m tryng to do something like this:
我正在尝试做这样的事情:


I have a List with my own object, in this List I have all the information to draw it, I have a Listbox and every figure is an item of Listbox that I draw in a position of the canvas.
我有一个带有我自己对象的列表,在这个列表中我有绘制它的所有信息,我有一个列表框,每个图形都是我在画布位置绘制的列表框的一个项目。
My problem is when I want to draw the Name, becuase I have to rotate the name and to draw it inside of the rectangle of the name, but when I rotate the name I don′t know how to draw it inside de rectangle.
我的问题是当我想绘制名称时,因为我必须旋转名称并将其绘制在名称的矩形内,但是当我旋转名称时,我不知道如何将其绘制在矩形内。
I′m using this datatemplate for my listbox ItemTemplate, I put visibility depending on if I want to draw the image or name.
我正在为我的列表框 ItemTemplate 使用这个数据模板,我根据是否要绘制图像或名称来设置可见性。
<DataTemplate x:Key="templateList">
<Canvas >
<WrapPanel
Visibility="{Binding IsStone, Converter={StaticResource converterCheckIsVisibleName}}"
Width="{Binding ObjectName.Width}"
Height="{Binding ObjectName.Height}">
<TextBlock Text="{Binding ObjectName.Name}" >
<TextBlock.RenderTransform>
<RotateTransform Angle="{Binding ObjectName.Rotate}" />
</TextBlock.RenderTransform>
</TextBlock>
</WrapPanel>
<Border BorderThickness="1" BorderBrush="Black"
Visibility="{Binding IsStone, Converter={StaticResource converterCheckIsVisibleStone}}">
<Image Source="{Binding ObjectIMG.PathImagen}"
Height="{Binding ObjectIMG.Height}"
Width="{Binding ObjectIMG.Width}"
Stretch="Fill" />
</Border>
</Canvas>
</DataTemplate>
回答by Sheridan
I'm not 100% sure what your problem is, but it seems as though you just want to rotate some text and put it into a rectangle. The Rectangleclass does not take a content element, so you can't use that, but you canuse a simple Borderelement.
我不是 100% 确定您的问题是什么,但似乎您只想旋转一些文本并将其放入矩形中。本Rectangle类并不需要一个内容元素,所以你不能使用,但你可以用一个简单的Border元素。
One other thing to notice is that you should probably use the LayoutTransformwhich occurs beforethe layout pass, as opposed to the RenderTransformwhich is applied afterwards. See the FrameworkElement.LayoutTransform Propertypage on MSDN for more information on this. Try something like this:
要注意的另一件事是,您可能应该使用LayoutTransformwhich在布局传递之前发生,而不是在RenderTransform之后应用 which。有关更多信息,请参阅MSDN 上的FrameworkElement.LayoutTransform 属性页。尝试这样的事情:
<Border BorderBrush="Black" BorderThickness="5">
<TextBlock Text="{Binding ObjectName.Name}" >
<TextBlock.LayoutTransform>
<RotateTransform Angle="{Binding ObjectName.Rotate}" />
</TextBlock.LayoutTransform>
</TextBlock>
</Border>

