使用画布将文本置于 Silverlight 形状的中心
我有一个画布控件,其中包含一个形状和一些文本。
<Canvas x:Name="myCanv">
<Rectangle x:Name="myRect" Stroke="Gray" Fill="Blue" StrokeThickness="3" Width="675" Height="350"/>
<TextBlock x:Name="myText"/>
</Canvas>
在后面的代码中,我将在事件发生时更改文本,我认为将文本放置在“myRect”的中心会很整洁。
我在 如何使用 MultiBinding 转换器将 WPF 应用程序中的画布控件中的文本居中。读完后,我在 WP7 上尝试了一下,结果痛苦!
不幸的是,Silverlight 4(WP7 Silverlight 是其子集)似乎没有 MultiBinding 转换器。然后我发现一个博客在 SL4 中有 MbC 代码,但我无法在 VS2010 Express 中打开该项目。
有没有人
A) 通过值转换器居中文本
或
B)找到了一种更好的方法来使文本居中而不使用值转换器
......? :)
I have a canvas control which I'm housing a shape and some text in.
<Canvas x:Name="myCanv">
<Rectangle x:Name="myRect" Stroke="Gray" Fill="Blue" StrokeThickness="3" Width="675" Height="350"/>
<TextBlock x:Name="myText"/>
</Canvas>
In my code behind i'll be changing the text as events occur and I thought it'd be neat to place the text in the center of "myRect".
I found a great blog post on how to center text in a canvas control in WPF app using MultiBinding Converters. After reading it, I tried it out for WP7 only to be met with pain!
Unfortunately, it seems as though Silverlight 4 (of which WP7 Silverlight is a subset) doesn't have MultiBinding Converters. I then found a blog which has code for MbC in SL4, but i can't open the project in VS2010 Express.
Has anyone either
A) Centered text via a value converter
or
B) found a better way to center the text without using a value converter
...? :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不如使用
Border
来代替,这样您就可以简单地将 TextBlock 的水平和垂直对齐属性设置为居中。How about using a
Border
instead, that way you can simple set the Horizontal and Vertical Alignment properties of the TextBlock to Center.您可以将文本框放在可视树中画布的外部,但绝对将其定位到相同的大小和位置。然后,您可以在文本块上设置水平和垂直对齐方式,以便文本看起来就像在画布中居中一样。
You could just put the textbox outside the canvas in the visual tree but absolutely position it to the same size and location. You could then set the horizontal and vertical alignment on the textblock so that the text appears as if centered in the canvas.